我有一个堆积酒吧阴谋使用ggplot我试图建立一个特定的颜色配置。我有两个因素,“Group.2”有6个级别,由单个条形表示,而“Group.1”有49个级别,由条形内的堆叠部分表示。我还有一个连续变量“x”,用于定义每个条的整体大小(包括所有堆叠元素)。我在下面提供了一个虚拟数据的例子。ggplot堆积条形图 - 如何将不同长度的色彩坡道应用于每个条形图?
我可以构建情节,但我不能按照我想要的方式获得配色方案。我想用不同的颜色斜坡填充每个栏(即“Group.1”的级别)(因此下面的示例中为6个颜色斜坡)。我希望每个栏中的堆叠部分都用各自颜色渐变中的插值颜色填充。由于每个酒吧有不同数量的堆叠元素,我希望颜色坡道长度不同,与堆叠元素的数量成比例(即每个级别的“Group.1”级别的数量“ Group.2" )。
这里的数据:
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")
这里是基本的情节(在这个例子中,所有6条具有相同的颜色渐变):
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")
这里有6个颜色坡道为例(使用颜色取自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)
我试图把彩色斜坡到一个列表:
rampList <- list(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,Group.2,x)”来重新排列数据框的“Group.2”,然后我在调用“reorder(Group.1,-x)”的“ “geom_bar”中的“组”来订购堆叠元素。 – Steve 2012-01-16 02:25:39
太棒了,很高兴它的作品。啊,我现在看到我的颜色在右边黑暗,但你的左边是黑暗的。很好的调整! – 2012-01-16 03:21:45