【问题标题】:Consistent mapping from value to color in ggplotggplot中从值到颜色的一致映射
【发布时间】:2021-06-23 03:44:17
【问题描述】:

我想我在这里遗漏了一些非常简单的东西,但我现在无法弄清楚: 我想始终如一地将颜色分配给跨多个图的列中的某些值。 所以我有这个小标题(sl):

# A tibble: 15 x 3
   class                           hex         x
   <chr>                           <chr>   <int>
 1 translational slide             #c23b22     1
 2 rotational slide                #AFC6CE     2
 3 fast flow-type                  #b7bf5e     3
 4 complex                         #A6CEE3     4
 5 area subject to rockfall/topple #1F78B4     5
 6 fall-type                       #B2DF8A     6
 7 n.d.                            #33A02C     7
 8 NA                              #FB9A99     8
 9 area subject to shallow-slides  #E31A1C     9
10 slow flow-type                  #FDBF6F    10
11 topple                          #FF7F00    11
12 deep-seated movement            #CAB2D6    12
13 subsidence                      #6A3D9A    13
14 areas subject to subsidence     #FFFF99    14
15 area of expansion               #B15928    15

这应该重新创建它:

structure(list(class = c("translational slide", "rotational slide", 
"fast flow-type", "complex", "area subject to rockfall/topple", 
"fall-type", "n.d.", NA, "area subject to shallow-slides", "slow flow-type", 
"topple", "deep-seated movement", "subsidence", "areas subject to subsidence", 
"area of expansion"), hex = c("#c23b22", "#AFC6CE", "#b7bf5e", 
"#A6CEE3", "#1F78B4", "#B2DF8A", "#33A02C", "#FB9A99", "#E31A1C", 
"#FDBF6F", "#FF7F00", "#CAB2D6", "#6A3D9A", "#FFFF99", "#B15928"
), x = 1:15), row.names = c(NA, -15L), class = c("tbl_df", "tbl", 
"data.frame"))

现在,如果每个类的十六进制代码(现在仅用于可视化目的),我想用颜色中的条绘制每个类。所以我做了:

ggplot(sl) +
  geom_col(aes(x = x,
               y = 1,
               fill = class)) +
  scale_fill_manual(values = sl$hex) +
  geom_text(aes(x = x,
                y = 0.5,
                label = class),
            angle = 90)

但这些不是 tibble 中的颜色。 所以我试着按照这个指南:How to assign colors to categorical variables in ggplot2 that have stable mapping? 并创建了这个:

# create the color palette
mycols = sl$hex 
names(mycols) = sl$class

然后用它绘制

ggplot(sl) +
  geom_col(aes(x = x,
               y = 1,
               fill = class)) +
  scale_fill_manual(values = mycols) +
  geom_text(aes(x = x,
                y = 0.5,
                label = class),
            angle = 90)

但结果是一样的。是这样的:

例如,平移幻灯片具有十六进制代码:“#c23b22”,应该是柔和的深红色。 任何人都可能知道我在这里缺少什么?

【问题讨论】:

标签: r ggplot2 color-palette


【解决方案1】:

考虑一下:


sl <- structure(list(class = c("translational slide", "rotational slide",
"fast flow-type", "complex", "area subject to rockfall/topple",
"fall-type", "n.d.", NA, "area subject to shallow-slides", "slow flow-type",
"topple", "deep-seated movement", "subsidence", "areas subject to subsidence",
"area of expansion"), hex = c("#c23b22", "#AFC6CE", "#b7bf5e",
"#A6CEE3", "#1F78B4", "#B2DF8A", "#33A02C", "#FB9A99", "#E31A1C",
"#FDBF6F", "#FF7F00", "#CAB2D6", "#6A3D9A", "#FFFF99", "#B15928"
), x = 1:15), row.names = c(NA, -15L), class = c("tbl_df", "tbl",
"data.frame"))

sl$class <- factor( sl$class, levels=unique(sl$class) )

cl <- sl$hex
names(cl) <- paste( sl$class )

ggplot(sl) +
    geom_col(aes(x = x,
                 y = 1,
                 fill = class)) +
    scale_fill_manual( values = cl, na.value = cl["NA"] ) +
    geom_text(aes(x = x,
                  y = 0.5,
                  label = class),
              angle = 90)

通过将 class 更改为一个因子并为其设置级别,并在 scale_fill_manual 中为您的值使用命名向量,并在其中正确使用 na.value,您可能会得到看起来更符合预期的东西。

【讨论】:

    【解决方案2】:

    我认为问题在于scale_fill_manual 期望其values 和labels 参数的顺序 匹配。您的数据集并非如此。

    会

    sl %>% ggplot() +
      geom_col(aes(x = x,
                   y = 1,
                   fill = hex)) +
      geom_text(aes(x = x,
                    y = 0.5,
                    label = class),
                angle = 90) +
      scale_fill_manual(values=sl$hex, labels=sl$class)
    

    给你你想要的?

    下一次,请dput()你的测试数据:我花了很长时间来创建测试数据集来回答你的问题。此外,使用颜色的十六进制代码很难检查颜色是否符合预期。对于 MWE,blue/green/black etx 会更有帮助。

    【讨论】:

    • 非常感谢!我想dput() 数据框,但我完全忘记了。我现在添加它。但是,我认为,仅通过查看,颜色就不是十六进制代码中的颜色。
    【解决方案3】:

    您需要根据您的专栏为颜色提供正确的顺序,因为已经有一个名为“x”的我也使用过它。我也用字符'NA'替换了NA。我检查了其中一些,如果这不是所需的输出,请告诉我。谢谢

    #Assuming df is your dataframe:
    
    df[is.na(df$class), 'class'] <- 'NA'
    
    ggplot(df) +
    geom_col(aes(x = x,
               y = 1,
               fill = factor(x))) +
    scale_fill_manual(values = df$hex, labels=df$class) +
    geom_text(aes(x = x,
                y = 0.5,
                label = class),
            angle = 90)
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多