本文介绍了ggplot堆积条形图 - 如何将不同长度的色彩坡道应用于每个条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个使用ggplot的堆积条形图,我试图构建一个特定的颜色配置。我有两个因素,Group.2有6个级别,由单个条形表示,而Group.1有49个级别,由条形内的堆叠部分表示。我还有一个连续变量x,用于定义每个条的整​​体大小(包括所有堆叠元素)。我在下面提供了一个虚拟数据的例子。 我可以构造这个图,但是我无法按照我想要的方式获得配色方案。我想用不同的颜色斜坡填充每个栏(即Group.1的级别)(因此下面的示例中为6个颜色斜坡)。我希望每个栏中的堆叠部分都用各自颜色渐变中的插值颜色填充。由于每个酒吧有不同数量的堆叠元素,我希望颜色坡道长度不同,与堆叠元素的数量成比例(即每个级别的Group.1级别的数量第2组)。 以下是数据: dat < - 结构(list(Group.1 = structure(1:49,.Label = c(1,2,3,4,5,6, 7,8,9,10,11,12,13,14,,15,16,17,18 19,20,21,22,23,24,25,,26,27,28,29, 30,31,32,33,34,35,36,,37,38,39,40,41 ,42,43,44,45,46,47,48,49),class =factor结构(c(6L,6L, 6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L,6L, 6L, 6L,6L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,1L, 1L,1L,1L,1L,1L,1L,1L,3L, 3L,4L,4L,4L,5L,5L),标签= c(苹果,香蕉,金橘,甜瓜,橙色 Pear),class =factor),x = c(3,1,1,4,41,3.5,3,26,0.3333333333,3,8.5,1, 4,1 ,0.25,3.5,4,6,4.5,3,2,10,62,16,12,11.5,19.66666667, 3,2.5,3.5,5,0.5,1,38,12,20,4.3333333333 ,3.5%,1, 1,0.25,20.33333333,1.5%,2,3,3,1,3,1),XMAX = C(123.5833333, 123.5833333,123.5833333,123.5833333,123.5833333,123.5833333, 123.5833333,123.5833333,123.5833333,123.5833333,123.5833333, 123.5833333,123.5833333,123.5833333,123.5833333,123.5833333, 123.5833333,123.5833333,123.5833333,123.5833333,123.5833333, 146.6666667,146.6666667, 146.6666667,146.6666667,146.6666667, 146.6666667,146.6666667,146.6666667,146.6666667,146.6666667, 146.6666667,146.6666667,80.08333333,80.08333333,80.08333333, 80.08333333,80.08333333,80.08333333,80.08333333,80.08333333 23.83333333,23.83333333,23.83333333,7,7,7,4,4),xCum = C(3, 4,5,9,50,53.5,56.5,82.5,82.83333333,85.8 3333333, 94.33333333,95.33333333,99.33333333,100.3333333,100.5833333, 104.0833333,108.0833333,114.0833333,118.5833333,121.5833333, 123.5833333,10,72,88,100,111.5,131.1666667,134.1666667, 136.6666667,140.1666667,145.1666667,145.6666667,146.6666667, 38,50,70,74.33333333,77.83333333,78.83333333,79.83333333, 80.08333333,20.33333333,21.83333333,23.83333333,3,6, 7,3,4),n = c(21L,21L,21L,21L,21L,21L,21L,21L,21L,21L, 21L,21L,21L,21L,21L,21L,21L,21L ,21L,21L,21L,21L, 12L,12L,12L,12L,12L,12L,12L,12L,12L,12L,12L,12L,12L, 8L,8L,8L,8L,8L ,8L,8L,8L,3L,3L,3L,3L,3L,3L,2L, 2L)),.Names = c(Group.1,Group.2,x xMax,xCum,n),row.names = c(NA,-49L),class =data.frame) pre> 下面是基本图(在这个例子中,所有的6条线具有相同的颜色斜坡): library(ggplot2) ggplot(data = dat,aes (x = reorder(Group.2,-xMax),y = x))+ geom_bar(aes(fill = reorder(Group.1,-x)),position ='stack',color =black ),size = .3)+ coord_flip()+ theme_bw()+ stat_bin(geom =text,aes(x = reorder(Group.2,-xMax)), y = xMax + 5,label = ..count ..), size = 3.5,color =grey10)+ scale_fill_manual(Group.2,values = colorRampPalette(c( bias = 0.3,space =Lab,interpolate =linear)(49))+ opts(legend.position =none) 下面是6个颜色渐变的示例(使用从RColorBrewer获取的颜色)。 r1 r2 r3 r4 R6< - colorRampPalette (c(#FCCDE5,#BC80BD),bias = 0.3,space =Lab,interpolate =linear)(21) 我尝试将颜色渐变放入列表中: rampList< ; - 列表(r1 = r1,r2 = r2,r3 = r3,r4 = r4,r5 = r5,r6 = r6) ,然后在调用中使用列表 scale_fill_manual(Group.2,values = rampList) 但它不接受列表。任何人都可以提出一种方法来解决这个问题吗理想情况下,我想要一个可以推广的解决方案,因为我需要使用具有不同数量的Group.1和Group.2级别的数据。 编辑1:以下是我想要使用Inkscape的一个模拟示例。请注意,每个酒吧都有自己的个人颜色渐变: 解决方案诀窍是自己计算颜色,就像您一样,然后将它们添加到 dat dataframe。然后你可以使用 scale_fill_identity()来直接使用它们。就像这样: $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ col = cols library(ggplot2) ggplot(data = dat,aes(x = reorder(Group.2,-xMax),y = x))+ geom_bar ()= coord_flip()+ theme_bw()+ stat_bin(geom =text,aes(x = reorder(Group.2,-xMax),y = xMax + 5,label = ..count ..), size = 3.5,color = grey10)+ scale_fill_identity()+ opts(legend.position =none) I have a a stacked bar plot using ggplot for which I'm trying to construct a particular color configuration. I have two factors, "Group.2" has 6 levels that are represented by the individual bars, while "Group.1" has 49 levels that are represented by the stacked sections within bars. I also have a continuous variable "x" that defines the overall size of each bar (including all stacked elements). I've provided an example with dummy data below.I can construct the plot, but I can't get the color scheme the way I want it. I'd like to fill each bar (i.e., the levels of "Group.1") using a different color ramp (so 6 color ramps in the example below). I'd like the stacked sections within each bar to be filled by the interpolated colors in their respective color ramps. Since each of the bars has a different number of stacked elements, i'd like the color ramps to be different lengths, proportional to the number of stacked elements (i.e., the number of levels of "Group.1" for each level of "Group.2").Here's the data:dat <- structure(list(Group.1 = structure(1:49, .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49"), class = "factor"), Group.2 = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L), .Label = c("Apple", "Banana", "Kumquat", "Melon", "Orange", "Pear"), class = "factor"), x = c(3, 1, 1, 4, 41, 3.5, 3, 26, 0.333333333, 3, 8.5, 1, 4, 1, 0.25, 3.5, 4, 6, 4.5, 3, 2, 10, 62, 16, 12, 11.5, 19.66666667, 3, 2.5, 3.5, 5, 0.5, 1, 38, 12, 20, 4.333333333, 3.5, 1, 1, 0.25, 20.33333333, 1.5, 2, 3, 3, 1, 3, 1), xMax = c(123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 123.5833333, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 146.6666667, 80.08333333, 80.08333333, 80.08333333, 80.08333333, 80.08333333, 80.08333333, 80.08333333, 80.08333333, 23.83333333, 23.83333333, 23.83333333, 7, 7, 7, 4, 4), xCum = c(3, 4, 5, 9, 50, 53.5, 56.5, 82.5, 82.83333333, 85.83333333, 94.33333333, 95.33333333, 99.33333333, 100.3333333, 100.5833333, 104.0833333, 108.0833333, 114.0833333, 118.5833333, 121.5833333, 123.5833333, 10, 72, 88, 100, 111.5, 131.1666667, 134.1666667, 136.6666667, 140.1666667, 145.1666667, 145.6666667, 146.6666667, 38, 50, 70, 74.33333333, 77.83333333, 78.83333333, 79.83333333, 80.08333333, 20.33333333, 21.83333333, 23.83333333, 3, 6, 7, 3, 4), n = c(21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L)), .Names = c("Group.1", "Group.2", "x", "xMax", "xCum", "n"), row.names = c(NA, -49L), class = "data.frame")Here's the basic plot (in this example, all 6 bars have the same color ramp):library(ggplot2)ggplot(data = dat, aes(x = reorder(Group.2, -xMax), y = x) ) + geom_bar( aes(fill = reorder(Group.1, -x)), position='stack', colour="black", size=.3) + coord_flip() + theme_bw() + stat_bin(geom = "text", aes(x = reorder(Group.2, -xMax), y = xMax+5, label = ..count..), size = 3.5, colour = "grey10") + scale_fill_manual("Group.2", values = colorRampPalette(c("#FDB462", "#FB8072"), bias=0.3, space = "Lab", interpolate = "linear")(49) ) + opts(legend.position = "none") Here's an example of 6 color ramps (using colors taken from RColorBrewer).r1 <- colorRampPalette(c("#FDB462", "#FB8072"), bias=0.3, space = "Lab", interpolate = "linear")(8)r2 <- colorRampPalette(c("#FFFFB3", "#FFED6F"), bias=0.3, space = "Lab", interpolate = "linear")(12)r3 <- colorRampPalette(c("#CCEBC5", "#B3DE69"), bias=0.3, space = "Lab", interpolate = "linear")(3)r4 <- colorRampPalette(c("#8DD3C7", "#80B1D3"), bias=0.3, space = "Lab", interpolate = "linear")(3)r5 <- colorRampPalette(c("#BEBADA", "#D9D9D9"), bias=0.3, space = "Lab", interpolate = "linear")(2)r6 <- colorRampPalette(c("#FCCDE5", "#BC80BD"), bias=0.3, space = "Lab", interpolate = "linear")(21)I've tried putting the color ramps into a list:rampList <- list(r1=r1, r2=r2, r3=r3, r4=r4, r5=r5, r6=r6)and then using the list in the call toscale_fill_manual("Group.2", values = rampList)but it won't accept a list. Can anyone suggest a way to approach this? Ideally, i'd like a solution that will generalize, as I will need to use the plot with data that have varying numbers of "Group.1" and "Group.2" levels.Edit 1: Here's a mocked up example of what i'd like using Inkscape. Notice each bar has it's own individual color ramp: 解决方案 The trick is to calculate the colors yourself, like you were doing, and then add them into your dat dataframe. Then you can use scale_fill_identity() to use them directly. Something like this:cols = c(r6, r2, r1, r3, r4, r5)dat$col = colslibrary(ggplot2)ggplot(data = dat, aes(x = reorder(Group.2, -xMax), y = x)) + geom_bar( aes(group=Group.1, fill=col), position='stack', colour="black", size=.3) + coord_flip() + theme_bw() + stat_bin(geom = "text", aes(x = reorder(Group.2, -xMax), y = xMax+5, label = ..count..), size = 3.5, colour = "grey10") + scale_fill_identity() + opts(legend.position = "none") 这篇关于ggplot堆积条形图 - 如何将不同长度的色彩坡道应用于每个条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 09:14