在2个不同的ggplot stat_summary函数中更改颜色

时间:2016-11-01 18:14:24

标签: r ggplot2

我的问题是this one的扩展 但我在我的ggplot中使用了2个stat_summary调用。第一个stat_summary显示每个单位的平均值,第二个stat_summary显示多个单位的组(时间)的总体平均值(参见附图)。 使用color = factor(unit)并使用scale_colour_manual()指定,分配第一个stat_summary的颜色没有问题。我想在第二个stat_summary中通过2个时间级别(pre和pst)分配颜色,例如color = time,但我不能再使用scale_colour_manual来指定颜色。         我试过了:         color = c(" red"," blue"),         颜色= cbPalette2,         aes(color = paste(" mean",time)))(如相关问题所示),但没有运气。任何帮助将不胜感激。

数据:

 structure(list(time = c("pre", "pre", "pre", "pre", "pre", "pre","pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst"), unit = c(73, 72, 72, 73, 73, 75, 75, 75, 72, 72, 73,73, 75, 75, 72, 74, 71, 74, 74, 71, 71, 74, 74, 71, 72, 73, 72, 72, 73, 73, 72, 73, 72, 75, 75, 75, 75, 75, 71, 71, 71, 74, 74, 71, 74, 71, 70, 66, 68, 66, 68,70, 70, 66, 70, 68, 66, 68, 70, 67, 69, 69, 69, 67, 69, 67, 69, 66, 66, 66, 68, 68, 66, 68, 66, 68, 70, 70, 70, 70, 70, 67, 67, 69, 67, 69, 69, 67, 69, 67, 69, 76, 76, 76, 78, 78, 78, 76, 78, 78, 76, 77, 77, 77, 79, 79, 79, 77, 77, 79, 79, 78, 78, 78, 78, 76, 76, 76, 76, 76, 77, 79, 79, 77, 77, 79, 79, 77, 79), number = c(159, 152, 151, 157, 156, 169, 167, 166, 153, 155, 158, 160, 168, 170, 154, 164, 146, 162, 161, 150, 147, 165, 163, 149, 231, 236, 235, 233, 240, 238, 232, 237, 234, 246, 247, 248, 249, 250, 226, 230, 228, 245, 243, 227, 242, 229, 141, 123, 133, 122, 132, 143, 145, 121, 142, 131, 124, 134, 144, 130, 138, 140, 137, 126, 136, 129, 139, 201, 205, 203, 215, 213, 202, 212, 204, 214, 221, 222, 223, 224, 225, 206, 210, 216, 208, 220, 218, 207, 217, 209, 219, 174, 172, 171, 182, 184, 181, 173, 185, 183, 175, 177, 179, 176, 189, 186, 187, 180, 178, 190, 188, 262, 261, 265, 263, 251, 252, 253, 254, 255, 257, 269, 267, 256, 260, 266, 270, 258, 268), rep = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C"), tied = c("C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V"), measure = c(0.65, 0.32, 0.29, 0.53, 1.18, 0.62, 0.2, 0.38, 0.64, 0.29,0.45, 0.66, 0.18, 0.74, 0.36, 0.97, 0.27, 0.2, 0.5, 0.34, 0.19, 0.73, 0.58, 0.29, 0.47, 0.17, 0.75, 0.47, 0.37, 0.44, 0.79, 0.37, 0.51, 0.19, 0.28, 0.21, 0.18, 0.2, 0.01, 0.69, 0.66, 0.32, -0.05, 0.69, -0.0899999999999999, 0.52, 0.2, 0.17, 0.21, 0.2, 0.17, 0.31, 0.41, 0.38, 0.21, 0.47, 0.37, 0.29, 0.52, 0.26, 0.47, 0.27, 0.35, 1.14, 1.23, 0.81, 0.56, 0.64, 0.35, 0.43, 0.51, 0.44, 0.84, 0.59, 0.14, 0.55, 0.16, 0.13, 0.16, 0.15, 0.17, 0.31, 0.6, 0.22, 0.57, 0.65, 0.51, 0.77, 0.56, 0.51, 0.52, 0.2, 0.18, 0.03, 0.12, 0.37, 0.14, 0.1, 0.31, 0.16, 0.39, 0.2, 0.27, 0.17, 0.27, 0.14, 0.13, 0.28, 0.5, 0.23, 0.17, 0.05, 0.67, 0.4, 0.24, 0.18, 0.33, 0.22, 0.12, 0.15, 0.0700000000000001, 0, 0.02, 0.39, 0.41, 0.88, 0.28, -0.0600000000000001, -0.01)), .Names = c("time", "unit", "number", "rep", "tied", "measure"), row.names = c(NA,-129L), class = "data.frame")

代码:

 cbPalette2 <- c("ghostwhite", "lightsteelblue1", "lightyellow" )
 unitcolours1<-c("purple","gold", "#999999","#009E73","plum1","#E69F00","#56B4E9","black","lightgreen","red","#0072B2","darkolivegreen3","#CC79A7","deeppink4")


 v<-ggplot(stack, aes(x=tied, y=measure, fill=time)) + 

 geom_point(size=0.5, aes(fill=factor(time), colour=factor(unit)), position=position_jitterdodge(jitter.width = NULL, jitter.height = 0, dodge.width = 0.75))+

    stat_summary(mapping=aes(shape=95, fill=time, colour=factor(unit)), fun.y= "mean", geom="point", size=6, position=position_dodge(width=0.75))+

    stat_summary(mapping=aes(shape=95, fill=time) , fun.y= "mean", geom="point", size=16, position=position_dodge(width=0.75))+

 facet_grid(.~rep)+
 labs(fill="time",  x="tied", y="measure (cP)")+
 scale_fill_manual(values=cbPalette2) +
 scale_colour_manual(values=unitcolours1)+
 guides(colour=FALSE)+
 scale_shape_identity() +
 theme_bw()+
 scale_y_continuous(limits=c(-0.1,1.24), breaks=seq(0, 1.2, 0.1))

1 个答案:

答案 0 :(得分:0)

正如我在评论中指出的那样,我不确定您希望绘图的样子,但是您可以通过使用填充标记(例如形状21 - 25)为一个geom和非ge来获得单独的颜色映射填充另一个标记。然后,您可以将变量映射到填充标记的填充美学,并将另一个变量映射到非填充标记的颜色美学。

以下是使用修改后的代码版本的示例。这可能不是你想到的那样,所以让我知道你实际上想要做什么,并且我将根据需要进行修改。

pd = position_dodge(0.75)

ggplot(stack, aes(x=tied, y=measure)) + 
  geom_point(size=1, aes(fill=time), shape=21, colour=NA, 
             position=position_jitterdodge(jitter.width=NULL, jitter.height=0, dodge.width=0.75)) +
  stat_summary(shape=95, fun.y=mean, geom="point", size=12, position=pd) +
  stat_summary(shape=95, aes(colour=factor(unit)), fun.y=mean, geom="point", size=8, position=pd) +
  facet_grid(.~rep) +
  labs(fill="Time", x="tied", y="measure (cP)") +
  # scale_fill_manual(values=cbPalette2) +
  # scale_colour_manual(values=unitcolours1) +
  guides(colour=FALSE) +
  #scale_shape_identity() +
  theme_bw() +
  scale_y_continuous(limits=c(-0.1,1.24), breaks=seq(0, 1.2, 0.1))

enter image description here