【问题标题】:How to plot a stacked barplot in ggplot2, with different shades of the given color如何在ggplot2中绘制一个堆叠的条形图,给定颜色的不同深浅
【发布时间】:2015-05-28 22:06:46
【问题描述】:

我有一个数据集 Region 如下。

Region   Color    0-25    25-50     50-75   75-100

AF       Green      51%     14%     24%     11%

AP       Red        5%      12%     9%      74%

EU       Yellow     18%     3%      36%     43%

Global   Green      34%     11%     19%     36%

LA       Green      44%     23%     22%     11%

NA       Green      100%    0%      0%      0%

我想为上述数据集使用 ggplot 绘制堆积条形图。 我正在寻找:,比如说当绘制全局数据时,我希望颜色为绿色(0-25 之间的范围),深绿色(25-50),浅灰色(50-75)和深灰色(75-100)之间。 其他类似,AP区域为红色、深红色、浅灰色和深灰色。

类似的东西:this post

这方面的任何提示都会有很大帮助。谢谢..

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是一种方法,使用绿色。

    dput(df)
    structure(list(Region = structure(c(1L, 2L, 3L, 4L, 5L, NA), .Label = c("AF", 
    "AP", "EU", "Global", "LA", "Region"), class = "factor"), Color = structure(c(2L, 
    3L, 4L, 2L, 2L, 2L), .Label = c("Color", "Green", "Red", "Yellow"
    ), class = "factor"), `0-25` = c(51, 5, 18, 34, 44, 100), `26-50` = c(14, 
    12, 3, 11, 23, 0), `51-75` = c(24, 9, 36, 19, 22, 0), `76-100` = c(11, 
    74, 43, 36, 11, 0)), .Names = c("Region", "Color", "0-25", "26-50", 
    "51-75", "76-100"), row.names = 2:7, class = "data.frame")
    

    然后,melt 充当tidyrcut 创建颜色因子。

    df.m <- melt(df, id.vars = "Region", measure.vars = 3:6)
    df.m$color.bin <- cut(x = df.m$value, breaks = c(0, 26, 51, 76, 100))
    df.m$color.bin <- as.factor(df.m$color.bin)
    

    评论后编辑

    然后我添加了一个因子变量供 ggplot 使用。

    df.m$shade <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "p","p","p","p","p","p","p","p")
    

    最后一步是第一次运行 ggplot 和 stacking 并手动设置填充颜色。虽然有六个区域,但我只匹配了前四个的配色方案。你可以完成剩下的。我添加了color 参数来显示堆叠段之间的划分。我添加了guides 调用来消除字母和颜色的长传说:

    ggplot(df.m, aes(x = Region, y = value, fill = shade)) +  
      geom_bar(stat = "identity", position = "stack", color = "black") + 
      scale_fill_manual(values = c("forestgreen", "lightcoral", "lemonchiffon", "blue", "grey30", "grey30",
                                   "lightgreen", "lightcoral", "lemonchiffon", "lightblue", "grey30", "grey30",
                                   "lightgreen","lightcoral", "yellow", "lightblue", "grey30", "grey30", 
                                   "lightgreen", "grey70","yellow", "blue", "grey30", "grey30")) +
      guides(fill = FALSE)
    

    【讨论】:

    • @LawyeR,感谢您的建议。但是我在看他的,对于区域 AP,颜色是 Red ,所以它应该是 0-25 的浅红色,然后是 25-50 和 50-75 灰色和 75-100 深灰色的深红色。我希望与数据框中的颜色相同。有可能吗
    • 但是 AP 只有两个颜色箱:0-25 和 51-76。所以只有两种颜色。将此行替换为最后一行代码,并看到浅红色(粉红色)和红色。 scale_fill_manual(values = c("pink", "red", "grey30", "grey70"))
    • @LawyeR,再次感谢您的回复和支持。是的,我需要用粉红色和红色为 AP 的两个引脚着色。而且我还需要 51-76 之间的区域为浅灰色和深灰色。欧盟是黄色的。我应该对每个条形图使用 scale_fill_manual(values = c("pale yellow", "yellow", "grey30", "grey70")) 吗?
    • 不,scale_fill 将用所选颜色为堆叠条的每个部分着色。您不需要单独处理每个条。选择您想要的任何四种颜色;但是,您必须拥有与切割数据相同的数量。这能回答你的问题吗?
    • 再次感谢您的支持。我想要的是根据数据集中的列颜色为每个条形着色不同的阴影。 AP是红色,EU是黄色。是否可以为我们想要的每个 geom_bar 添加不同的颜色。
    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多