John Mutuma John Mutuma - 1 month ago 11
R Question

How can I get rid of the blank space being left on the plot panel in R?

diamond subplot

I am using scale_x_discrete to set the limits of the x axis. I would like to know how to get rid of the blank space left on the panel and have the defined limits fill the entire axis.

myplot <- ggplot(diamonds, aes(x = clarity, fill = cut)) + #data
geom_bar() + #geom
scale_x_discrete(limits = c("I1", "SI2", "VS2",
name = "Clarity of Stones") #setting limits #limits
myplot

Answer

If your goal is to subset your data for particular measures (clarity in this case), you should do this within the data object provided. As in:

ggplot(diamonds[diamonds$clarity %in% c("I1", "SI2", "VS2"),], aes(x = clarity, fill = cut)) + 
  geom_bar()
Comments