简体   繁体   English

ggplot:geom_boxplot和geom_jitter

[英]ggplot: geom_boxplot and geom_jitter

Align the data points with the box plot. 将数据点与箱形图对齐。

DATA: 数据:

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))

CODES: 码:

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))

结果

As you can see, the data points plotted using geom_jitter do not align with the boxplot. 如您所见,使用geom_jitter绘制的数据点与箱线图不对齐。 I know that I need to provide aes elements to geom_jitter as well - but I not sure how to do it correctly. 我知道我也需要向geom_jitter提供aes元素-但我不确定如何正确地做到这一点。

I don't think you can do this because the positions of the boxplots are being driven by the dodge algorithm as opposed to an explicit aesthetic, though I'd be curious if someone else figures out a way of doing it. 我不认为您可以这样做,因为盒形图的位置是由躲闪算法驱动的,而不是由外在的美感决定的,尽管我很好奇是否有人想办法。 Here is a workaround: 这是一种解决方法:

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)

在此处输入图片说明

By using the facets for culture , we can assign an explicit aesthetic to status , which then allows to line up the geom_jitter with the geom_boxplot . 通过使用culture方面,我们可以为status赋予明确的美感,然后使geom_jittergeom_boxplot Hopefully this is close enough for your purposes. 希望这足够接近您的目的。

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

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