仅绘制某些箱图的抖动点

时间:2015-05-08 18:32:49

标签: r ggplot2 boxplot

我有五个矩阵形式的数据文件,我正在使用 geom_boxplot 绘制它。每个boxplot对应一个文件。

我想要实现的只是某些文件在这里说 div1,div3,div5 我想绘制boxplot,其中数据点覆盖在boxplot上。我可以使用 geom_jitter 添加数据点,但我必须将这些图表与唯一的箱图图表中的数据点分开。

由于我想保留绘制文件的顺序。 .i.e div0,div1 ..等等。我无法仅为某些箱图绘制数据点。

如何仅为某些箱图而不是所有箱图添加叠加数据点?

files <- c(div0,div1,div2,div3,div4,div5)

p1 <- ggplot(moltenNew,aes(x=L1,y=value,colour=L1))+ ylim(0.3,0.8) +
       geom_boxplot() + facet_wrap(~variable,nrow=1) + scale_x_discrete(limits = basename(files) ,labels = basename(files))


 ![enter image description here][1] 

1 个答案:

答案 0 :(得分:2)

您可以使用subset

set.seed(1)
moltenNew <- rbind(
  data.frame(value = rnorm(20, 50, 20), L1 = gl(2, 10), variable = 1),
  data.frame(value = rnorm(20, 100, 100), L1 = gl(2, 10), variable = 2),
  data.frame(value = rnorm(20, 75, 10), L1 = gl(2, 10), variable = 3) 
)
moltenNew
library(ggplot2)
ggplot(moltenNew,aes(x=L1,y=value,colour=L1)) + 
  geom_boxplot() + 
  facet_wrap(~variable,nrow=1, scale = "free_y") + 
  geom_point(subset = .(variable == 2), position = position_jitter(width = .2))

enter image description here