繁体   English   中英

ggplot:geom_boxplot和geom_jitter

[英]ggplot: geom_boxplot and geom_jitter

将数据点与箱形图对齐。

数据:

data<-structure(list(score = c(0.058, 0.21, -0.111, -0.103, 0.051, 
0.624, -0.023, 0.01, 0.033, -0.815, -0.505, -0.863, -0.736, -0.971, 
-0.137, -0.654, -0.689, -0.126), clin = structure(c(1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L), .Label = 
c("Non-Sensitive", 
"Sensitive "), class = "factor"), culture = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Co-culture", "Mono-culture"), class = "factor"), 
    status = structure(c(2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 
    2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L), .Label = c("new", "old"
    ), class = "factor")), .Names = c("score", "clin", "culture", 
"status"), class = "data.frame", row.names = c(NA, -18L))

码:

p<-ggplot(data, aes(culture, as.numeric(score),fill=status))

p+geom_boxplot(outlier.shape = NA)+
   theme_bw()+scale_fill_grey(start = 0.8, end = 1)+
   labs(title="title", x="", y="score",fill="", colour="")+
   geom_jitter(aes(colour = clin), alpha=0.9, 
   position=position_jitter(w=0.1,h=0.1))

结果

如您所见,使用geom_jitter绘制的数据点与箱线图不对齐。 我知道我也需要向geom_jitter提供aes元素-但我不确定如何正确地做到这一点。

我不认为您可以这样做,因为盒形图的位置是由躲闪算法驱动的,而不是由外在的美感决定的,尽管我很好奇是否有人想办法。 这是一种解决方法:

p<-ggplot(data, aes(status, as.numeric(score),fill=status))
p+geom_boxplot(outlier.shape = NA)+
  theme_bw()+scale_fill_grey(start = 0.8, end = 1)+
  labs(title="title", x="", y="score",fill="", colour="")+
  geom_jitter(aes(colour = clin), alpha=0.9, 
              position=position_jitter(w=0.1,h=0.1)) +
  facet_wrap(~ culture)

在此处输入图片说明

通过使用culture方面,我们可以为status赋予明确的美感,然后使geom_jittergeom_boxplot 希望这足够接近您的目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM