![](/img/trans.png)
[英]Plot a ggplot geom_boxplot with geom_jitter, separated by a factor and showing only box median
[英]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_jitter
與geom_boxplot
。 希望這足夠接近您的目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.