2015-02-23 124 views
7

ggplot2情節,我結合geom_linegeom_pointgeom_bar時遇到合併傳說到一個機箱中的問題。GGPLOT2:合併傳說爲geom_line,geom_point和geom_bar

守則的基本情節如下。使用的數據進一步下降。

# Packages 
library(ggplot2) 
library(scales) 

# Basic Plot 
ggplot(data = df1, aes(x = Year, y = value, group = variable, 
    colour = variable, shape = variable)) + 
geom_line() + 
geom_point(size = 3) + 
geom_bar(data = df2, aes(x = Year, y = value, fill = variable), 
    stat = "identity", alpha = 0.8) + 
ylab("Current Account Transactions (Billion $)") + 
xlab(NULL) + 
theme_bw(14) + 
scale_x_discrete(breaks = seq(1999, 2013, by = 2)) + 
scale_y_continuous(labels = dollar, limits = c(-1, 4), 
    breaks = seq(-1, 4, by = .5)) + 
geom_hline(yintercept = 0) + 
theme(legend.key = element_blank(), 
    legend.background = element_rect(colour = 'black', fill = 'white'), 
    legend.position = "top", legend.title = element_blank()) + 
guides(col = guide_legend(ncol = 1), fill = NULL, colour = NULL) 

enter image description here

我的目標是傳說融合在一起。出於某種原因,「經常賬戶餘額」出現在頂部的圖例中(我不明白爲什麼),而「出口」和「導入」圖例混淆了黑色背景和缺少形狀。

如果我走fillaes的我能得到傳說中的「進口」和「出口」,以顯示與正確的形狀和顏色,並沒有黑色背景,但後來我失去了fill傳說的「平衡經常帳戶「。

我取得了一些成功,這是在使用前已經使用的伎倆scale_colour_manualscale_shape_manualscale_fill_manual(也許scale_alpha)似乎並不在這裏工作。讓它工作會很好。但請注意,就我所知,使用這個技巧,必須手動指定顏色,形狀和填充,我並不想這麼做,因爲我對默認顏色/形狀/填充非常滿意。

我通常會做這樣的事情,但它不工作:

library(RColorBrewer) 
cols <- colorRampPalette(brewer.pal(9, "Set1"))(3) 
last_plot() + scale_colour_manual(name = "legend", values = cols) + 
    scale_shape_manual(name = "legend", values = c(0,2,1)) + 
    scale_fill_manual(name = "legend", values = "darkred") 

在上面,我不指定標籤,因爲在我的問題,我會處理大量數據,並手動指定標籤是不實際的。我想ggplot2使用默認標籤。出於同樣的原因,我想使用默認的顏色/形狀/填充。

類似的困難已在別處報道,例如這裏Construct a manual legend for a complicated plot,但我還沒有設法解決方案適用於我的問題。

任何想法?

# Data 

df1 <- structure(list(Year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L), .Label = c("1999", 
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", 
"2008", "2009", "2010", "2011", "2012", "2013"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Exports of goods and services", 
    "Imports of goods and services"), class = "factor"), value = c(1.304557, 
    1.471532, 1.345165, 1.31879, 1.409053, 1.642291, 1.895983, 
    2.222124, 2.569492, 2.751949, 2.285922, 2.630799, 2.987571, 
    3.08526, 3.178744, 1.600087, 1.882288, 1.740493, 1.776877, 
    1.930395, 2.276059, 2.641418, 3.028851, 3.288135, 3.43859, 
    2.666714, 3.074729, 3.446914, 3.546009, 3.578998)), .Names = c("Year", 
"variable", "value"), row.names = c(NA, -30L), class = "data.frame") 

df2 <- structure(list(Year = structure(1:15, .Label = c("1999", "2000 ", 
"2001", "2002 ", "2003", "2004 ", "2005", "2006 ", "2007", "2008 ", 
"2009", "2010 ", "2011", "2012 ", "2013"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = "Balance on current account", class = "factor"), 
    value = c(-0.29553, -0.410756, -0.395328, -0.458087, -0.521342, 
    -0.633768, -0.745434, -0.806726, -0.718643, -0.686641, -0.380792, 
    -0.44393, -0.459344, -0.460749, -0.400254)), .Names = c("Year", 
"variable", "value"), row.names = c(NA, -15L), class = "data.frame") 

編輯

發佈我的問題和閱讀斯科特的回答後,我嘗試用另一種方法。它在某些方面更接近理想的結果,但在其他方面更進一步。這個想法是把dataframes合併成一個單一的數據幀,並通過顏色/形狀/填充到aes第一ggplot呼叫內。這個問題是我在傳說中得到了一個不希望的「斜線」。如果不移除所有顏色,我一直無法刪除斜槓。這種方法的另一個問題,我馬上提到,我需要手動指定一些事情,而我想盡可能保持默認值。

df <- rbind(df1, df2) 
ggplot(data = df, aes(x = Year, y = value, group = variable, colour = variable, 
    shape = variable, fill = variable)) + 
    geom_line(data = subset(df, variable %in% c("Exports of goods and services", "Imports of goods and services"))) + 
    geom_point(data = subset(df, variable %in% c("Exports of goods and services", "Imports of goods and services")), size = 3) + 
    geom_bar(data = subset(df, variable %in% c("Balance on current account")), aes(x = Year, y = value, fill = variable), 
    stat = "identity", alpha = 0.8) 
cols <- c(NA, "darkgreen", "darkblue") 
last_plot() + scale_colour_manual(name = "legend", values = cols) + 
    scale_shape_manual(name = "legend", values = c(32, 15, 17)) + 
    scale_fill_manual(name = "legend", values = c("orange", NA, NA)) + 
    ylab("Current Account Transactions (Billion $)") + 
    xlab(NULL) + 
    theme_bw(14) + scale_x_discrete(breaks = seq(1999, 2013, by = 2)) + 
    scale_y_continuous(labels = dollar, limits = c(-1, 4), breaks = seq(-1, 4, by = .5)) + 
    geom_hline(yintercept = 0) + 
    theme(legend.key = element_blank(), legend.background = element_rect(colour = 'black', fill = 'white'), legend.position = "top", legend.title = element_blank()) + 
    guides(col = guide_legend(ncol = 1)) 

加入+ guides(fill = guide_legend(override.aes = list(colour = NULL)))刪除斜線但深綠/ darkblue顏色太(它保持橙色填充)。

enter image description here

回答

3

爲了消除出現在頂部的傳說你可以移動groupcolourshape美學走出父ggplot()呼叫,進入geom_line()geom_point()適當「當前賬戶餘額」。這可以對哪個美學適用於您的兩個共享變量名稱的數據集中的每一個提供具體的控制。 enter image description here

ggplot(data = df1, aes(x = Year, y = value)) + 
    geom_line(aes(group = variable, colour = variable)) + 
    geom_point(aes(shape = variable, colour = variable), size = 3) + 
    geom_bar(data = df2, aes(x = Year, y = value, fill = variable), 
      stat = "identity", position = 'identity', alpha = 0.8, guide = 'none') + 
    ylab("Current Account Transactions (Billion $)") + 
    xlab(NULL) + 
    theme_bw(14) + 
    scale_x_discrete(breaks = seq(1999, 2013, by = 2)) + 
    scale_y_continuous(labels = dollar, limits = c(-1, 4), 
        breaks = seq(-1, 4, by = .5)) + 
    geom_hline(yintercept = 0) + 
    guides(col = guide_legend(ncol = 1)) + 
    theme(legend.key = element_blank(), 
     legend.background = element_rect(colour = 'black', fill = 'white'), 
     legend.position = "top", legend.title = element_blank(), 
     legend.box.just = "left") 

這個答案有一些不足之處。給一對夫婦命名:1)兩個不同的傳說仍然存在,如果你決定不給他們裝箱,可以僞裝(例如,如果你沒有設置legend.background)。 2)從頂部圖例中刪除df2變量意味着它不會消耗第一個默認顏色(如前所述,僅僅是巧合),所以現在「Balance ...」和「Exports ...」都顯示爲粉紅色,因爲填充圖例回收默認色標。

+0

謝謝斯科特。這確實非常接近。顏色不應該太難修復。我想知道合併這兩個數據框並保持ggplot aes中的組/顏色/形狀/填充是否可行。我會試一試。 – PatrickT 2015-02-26 19:26:57

+0

看我的編輯。到目前爲止,您的答案比我的編輯更可取,儘管我的傳說中有很好的對齊方式,您的顏色/填充效果會更好。我只是注意到你的解決方案使用相同的形狀兩次,如果我看到正確,這是不可取的。再次感謝,我會繼續打開一會兒,以防萬一出現更好的情況。如果沒有,我會在幾天內接受你的回答。 – PatrickT 2015-02-26 20:51:13

+0

這個''geom_line(aes(group = variable,color = variable,shape = variable))''修復了代碼中的形狀。 – PatrickT 2015-02-27 16:10:10