绘图:基于两个列级别的组合的颜色

Prr*_*dep 2 plot r ggplot2

如何绘制基于两个柱水平(此处的组合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)
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

eip*_*i10 5

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)到绘图代码中。)

在此处输入图片说明