如何绘制基于两个柱水平(此处的组合treatment,replicate)?
set.seed(0)
x <- rep(1:10, 4)
y <- sample(c(rep(1:10, 2)+rnorm(20)/5, rep(6:15, 2) + rnorm(20)/5))
treatment <- sample(gl(8, 5, 40, labels=letters[1:8]))
replicate <- sample(gl(8, 5, 40))
d <- data.frame(x=x, y=y, treatment=treatment, replicate=replicate)
Run Code Online (Sandbox Code Playgroud)
图:基于单列级别的颜色
ggplot(d, aes(x=x, y=y, colour=treatment)) + geom_point()
Run Code Online (Sandbox Code Playgroud)
ggplot(d, aes(x=x, y=y, colour=replicate)) + geom_point()
Run Code Online (Sandbox Code Playgroud)
两个列级别的组合为a-1, a-2, a-3, ... h-6, h-7, h-8。
64种颜色将无法解释。如何标记点:
ggplot(d, aes(x=x, y=y, colour=treatment)) +
geom_text(aes(label=paste0(treatment, replicate)), size=3, show.legend=FALSE) +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
或者,如果您尝试发现不同治疗方式的差异,也许分面会有所帮助:
ggplot(d, aes(x=x, y=y, colour=treatment)) +
geom_text(aes(label=paste0(treatment, replicate)), size=3, show.legend=FALSE) +
facet_wrap(~ treatment, ncol=4) +
scale_x_continuous(expand=c(0,0.7)) +
theme_bw() + theme(panel.grid=element_blank())
Run Code Online (Sandbox Code Playgroud)
但是,如果您真的想要一大堆颜色...
ggplot(d, aes(x=x, y=y, colour=interaction(treatment,replicate,sep="-",lex.order=TRUE))) +
geom_point() +
labs(colour="Treatment-Replicate") +
theme_classic()
Run Code Online (Sandbox Code Playgroud)
(如果希望所有可能的treatment-replicate组合都在图例中列出,无论它们是否存在于数据中,请添加+ scale_colour_discrete(drop=FALSE)到绘图代码中。)
| 归档时间: |
|
| 查看次数: |
1768 次 |
| 最近记录: |