【问题标题】:ggplot2 - using two different color scales for overlayed plotsggplot2 - 对重叠图使用两种不同的色标
【发布时间】:2013-02-21 20:17:59
【问题描述】:

我试图覆盖两个不同的图。一个是geom_boxplot,另一个是geom_jitter。我希望每个都有自己的色标。但是当我添加第二个色标时,我得到了错误

 "Scale for 'fill' is already present. Adding another scale for 'fill', 
  which will replace the existing scale."

我假设我做错了什么。任何建议将不胜感激

这是我的工作代码的粗略示例:

P <-  ggplot(dat) + 
          geom_boxplot(aes(x=ve, y=metValue, fill=metric), alpha=.35, w=0.6, notch=FALSE, na.rm = TRUE) + 
          scale_fill_manual(values=cpalette1) + 
          geom_hline(yintercept=0, colour="#DD4466", linetype = "longdash") +
          theme(legend.position="none")

P + geom_jitter(dat2, aes(x=ve, y=metValue, fill=atd), 
                size=2, shape=4, alpha = 0.4, 
                position = position_jitter(width = .03, height=0.03), na.rm = TRUE) + 
              scale_fill_manual(values=cpalette2)

datdat2 具有相同的架构,但值不同。

我发现了几个解决重叠图的示例,但似乎没有一个解决此特定问题。

【问题讨论】:

    标签: r ggplot2 color-scheme


    【解决方案1】:

    首先,制作两个与示例同名的示例数据框。

    dat<-data.frame(ve=rep(c("FF","GG"),times=50),
                    metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25),
                    atd=rep(c("HH","GG"),times=50))
    dat2<-data.frame(ve=rep(c("FF","GG"),times=50),
                    metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25),
                    atd=rep(c("HH","GG"),times=50))
    

    我假设您不需要在geom_jitter() 中使用参数fill=,因为shape=4 的颜色也可以使用colour= 参数设置。然后您可以使用scale_colour_manual() 设置您的值。而不是cpallete 只是使用颜色名称。

    P <-  ggplot(dat) + 
      geom_boxplot(aes(x=ve, y=metValue, fill=metric), alpha=.35, w=0.6, notch=FALSE, na.rm = TRUE) +  
      geom_hline(yintercept=0, colour="#DD4466", linetype = "longdash") +
      scale_fill_manual(values=c("red","blue","green","yellow"))+
      theme(legend.position="none")
    
    P + geom_jitter(data=dat2, aes(x=ve, y=metValue, colour=atd), 
                    size=2, shape=4, alpha = 0.4, 
                    position = position_jitter(width = .03, height=0.03), na.rm = TRUE) + 
                    scale_colour_manual(values=c("red","blue"))
    

    【讨论】:

      猜你喜欢
      • 2016-01-29
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      相关资源
      最近更新 更多