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

时间:2017-06-02 16:41:42

标签: r plot 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)

绘图:基于单列级别的颜色

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

1 个答案:

答案 0 :(得分:4)

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