John Mutuma
John Mutuma

Reputation: 3600

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

This is the desired output: similar to output by subsetting

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

Upvotes: 1

Views: 368

Answers (2)

agenis
agenis

Reputation: 8377

Its probably just a syntax problem since you missed a parenthesis at the end of your limits argument, and the name of the scale is wrongly passed as a fourth column of your X-axis. You shoud run:

ggplot(diamonds, aes(x = clarity, fill = cut)) + geom_bar() + 
  scale_x_discrete(limits = c("I1", "SI2", "VS2"), name = "Clarity of Stones") 

Upvotes: 0

statespace
statespace

Reputation: 1664

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()

Upvotes: 1

Related Questions