rggplot2axis-labels

How to show levels with no data in boxplot?


I am trying to create a standard boxplot that shows the same levels regardless of what data is in the dataset. In the following example, how do I get the boxplot to show a level for Plum? All of the posts I can find mention the scale_x_discrete command with drop = FALSE, but that command doesn't seem to have any effect on my graph. The graph is the same with or without it.

fruit <- c("Apple","Apple","Orange","Orange","Peach")
numbr <- c(2,4,3,5,NA)
mydata <- data.frame(fruit,numbr)
fruitlist <- c("Apple","Orange","Peach","Plum")
ggplot(mydata, aes(x=fruit,y=numbr)) +
  geom_boxplot() +
  scale_x_discrete(breaks=fruitlist,labels=fruitlist, drop=FALSE)

enter image description here


Solution

  • Use a factor like the R developers intended:

    fruitlist <- c("Apple","Orange","Peach","Plum")
    mydata$fruit <- factor(mydata$fruit, levels = fruitlist) 
    
    ggplot(mydata, aes(x=fruit,y=numbr)) +
      geom_boxplot() +
      scale_x_discrete(drop = FALSE)