geom_boxplot() from ggplot2 : forcing an empty level to appear

此生再无相见时 提交于 2019-11-28 10:05:31

You can control the breaks in a suitable scale function, in this case scale_x_discrete. Make sure you use the argument drop=FALSE:

p <- ggplot(data=dftest[dftest$time!=2,],aes(x=factor(time,levels=1:10),y=value))
p + geom_boxplot() + 
  scale_x_discrete("time", breaks=factor(1:10), drop=FALSE)


I like to do my data manipulation in advance of sending it to ggplot. I think this makes the code more readable. This is how I would do it myself, but the results are the same. Note, however, that the ggplot scale gets much simpler, since you don't have to specify the breaks:

dfplot <- dftest[dftest$time!=2, ]
dfplot$time <- factor(dfplot$time, levels=1:10)

ggplot(data=dfplot, aes(x=time ,y=value)) +
    geom_boxplot() + 
    scale_x_discrete("time", drop=FALSE)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!