2012-01-15 72 views
1

我有一個堆積酒吧陰謀使用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的一個模擬的例子。注意每個酒吧都有自己單獨的顏色漸變:

barplot

回答

4

關鍵是要自己計算出的顏色,就像你在做什麼,然後將它們添加到您的dat數據幀。然後您可以使用scale_fill_identity()直接使用它們。事情是這樣的:

cols = c(r6, r2, r1, r3, r4, r5) 
dat$col = cols 

library(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") 

enter image description here

+0

感謝@約翰科爾比!這太棒了,我做了一些小小的調整,讓它達到我想要的效果。在添加顏色之前,我使用了「排列(dat,Group.2,x)」來重新排列數據框的「Group.2」,然後我在調用「reorder(Group.1,-x)」的「 「geom_bar」中的「組」來訂購堆疊元素。 – Steve 2012-01-16 02:25:39

+0

太棒了,很高興它的作品。啊,我現在看到我的顏色在右邊黑暗,但你的左邊是黑暗的。很好的調整! – 2012-01-16 03:21:45