gpt4 book ai didi

r - 绘制 : color based on the combination of two column levels

转载 作者:行者123 更新时间:2023-12-02 00:13:48 24 4
gpt4 key购买 nike

如何根据两个列级别的组合进行绘图(此处:treatmentreplicate)?

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)

绘图:基于单列水平的颜色

ggplot(d, aes(x=x, y=y, colour=treatment)) + geom_point()

output

ggplot(d, aes(x=x, y=y, colour=replicate)) + geom_point() 

output

两个列级别的组合将是 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()

enter image description here

或者,如果您试图找出不同治疗模式的差异,分面可能会有所帮助:

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

enter image description here

但是,如果你真的想要一大堆颜色......

ggplot(d, aes(x=x, y=y, colour=interaction(treatment,replicate,sep="-",lex.order=TRUE))) + 
geom_point() +
labs(colour="Treatment-Replicate") +
theme_classic()

(如果您希望在图例中列出所有潜在的treatment-replicate 组合,无论它们是否存在于数据中,然后添加 + scale_colour_discrete(drop=FALSE) 到剧情代码。)

enter image description here

关于r - 绘制 : color based on the combination of two column levels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44333757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com