【问题标题】:ggplot2: How to use same colors in different plots for same factorggplot2:如何在不同的图中为相同的因素使用相同的颜色
【发布时间】:2013-09-28 15:16:23
【问题描述】:

如何将相同的颜色固定到不同图中的值?

假设我有两个 data.frames df1 和 df2:

library(ggplot2)
library(gridExtra)

set.seed(1)
df1 <- data.frame(c=c('a', 'b', 'c', 'd', 'e'), x=1:5,  y=runif(5))
df2 <- data.frame(c=c('a', 'c', 'e', 'g', 'h'), x=1:5,  y=runif(5))

当使用 c 作为颜色指示器绘制它们时,我得到了相同的五种颜色。

g1 <- ggplot(df1, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity")
g2 <- ggplot(df2, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity")
grid.arrange(g1, g2, ncol=2)

但我希望 c 的相同值获得相同的颜色。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用scale_fill_manual 设置自己的填充比例。我用颜色和不同的“c”值创建了一个命名向量。

    dd <- union(df1$c,df2$c)
    dd.col <- rainbow(length(dd))
    names(dd.col)  <- dd
    

    然后:

    g1 <- ggplot(df1, aes(x=x, y=y, fill=c)) + 
      geom_bar(stat="identity") +
      scale_fill_manual("Legend", values = dd.col)
    g2 <- ggplot(df2, aes(x=x, y=y, fill=c)) + 
      geom_bar(stat="identity") +
      scale_fill_manual("Legend", values = dd.col)
    grid.arrange(g1, g2, ncol=2)
    

    【讨论】:

    • 所以我必须知道我将使用多少种不同的颜色。有没有办法在一个接一个地绘制每个图的过程中“收集”颜色?我的实际问题包括许多独立的情节,一开始我不知道我会得到多少(以及什么样的)“c”值。
    • 这个可以结合获取ggplot中默认调色板的方法:stackoverflow.com/questions/8197559/…。例如。 my_colors = gg_color_hue(length(my_factors))names(my_colors) &lt;- rev(my_factors) 并简单地将 scale_color_manual(values = my_colors) 添加到您的每个地块。
    • 我也有类似的问题。当我运行此代码时,两个图例都包含所有可能的组,而不仅仅是每个图中存在的组。有没有办法只包括传说中每个情节中存在的组?
    【解决方案2】:

    我现在写了一个函数来生成另一个计算颜色的函数。我不确定这是否是一个好方法。欢迎评论。

    library(ggplot2)
    library(gridExtra)
    library(RColorBrewer)
    
    makeColors <- function(){
      maxColors <- 10
      usedColors <- c()
      possibleColors <- colorRampPalette( brewer.pal( 9 , "Set1" ) )(maxColors)
    
      function(values){
        newKeys <- setdiff(values, names(usedColors))
        newColors <- possibleColors[1:length(newKeys)]
        usedColors.new <-  c(usedColors, newColors)
        names(usedColors.new) <- c(names(usedColors), newKeys)
        usedColors <<- usedColors.new
    
        possibleColors <<- possibleColors[length(newKeys)+1:maxColors]
        usedColors
      }
    } 
    
    mkColor <- makeColors()
    
    
    set.seed(1)
    df1 <- data.frame(c=c('a', 'b', 'c', 'd', 'e'), x=1:5,  y=runif(5))
    df2 <- data.frame(c=c('a', 'c', 'e', 'g', 'h'), x=1:5,  y=runif(5))
    
    g1 <- ggplot(df1, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity") + scale_fill_manual(values = mkColor(df1$c))
    g2 <- ggplot(df2, aes(x=x, y=y, fill=c)) + geom_bar(stat="identity") + scale_fill_manual(values = mkColor(df2$c))
    grid.arrange(g1, g2, ncol=2)
    

    【讨论】:

      【解决方案3】:

      要制作这种复合图,ggplot2 有多个方面:

      df1$id = 'A'
      df2$id = 'B'
      df_all = rbind(df1, df2)
      ggplot(df_all, aes(x=x, y=y, fill=c)) + 
          geom_bar(stat="identity") + 
          facet_wrap(~id)
      

      使用构面时,ggplot2 将两个图视为一个整体,保持颜色值映射相同。

      【讨论】:

      • 在我的示例中,我将两个图放在一起。我真正的问题包括许多独立的情节。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2019-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多