How to add boxplots to scatterplot with jitter

匿名 (未验证) 提交于 2019-12-03 01:55:01

问题:

I am using following commands to produce a scatterplot with jitter:

ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5],500,replace=T)) library(lattice) stripplot(NUMS~GRP,data=ddf, jitter.data=T) 

I want to add boxplots over these points (one for every group). I tried searching but I am not able to find code plotting all points (and not just outliers) and with jitter. How can I solve this. Thanks for your help.

回答1:

Here's one way using base graphics.

boxplot(NUMS ~ GRP, data = ddf, lwd = 2, ylab = 'NUMS') stripchart(NUMS ~ GRP, vertical = TRUE, data = ddf,      method = "jitter", add = TRUE, pch = 20, col = 'blue') 



回答2:

To do this in ggplot2, try:

ggplot(ddf, aes(x=GRP, y=NUMS)) +    geom_boxplot(outlier.shape=NA) + #avoid plotting outliers twice   geom_jitter(position=position_jitter(width=.1, height=0)) 

Obviously you can adjust the width and height arguments of position_jitter() to your liking (although I'd recommend height=0 since height jittering will make your plot inaccurate).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!