2014-11-04 157 views
8

我想用ggplot構成雙y軸圖表。首先讓我說,我並不是在討論這樣做是否是好的做法的優點。我發現它們在查看基於時間的數據以識別2個離散變量的趨勢時特別有用。對此的進一步討論更適合於我認爲的交叉驗證。如何在雙y軸ggplot上顯示圖例

Kohske提供了一個非常好的例子,說明如何做到這一點,我迄今爲止效果很好。然而,我在我的極限中包含了兩個y軸的圖例。我也看到類似的問題herehere,但似乎沒有解決包括圖例的問題。

我有一個使用ggplot的菱形數據集的可重現示例。

數據

library(ggplot2) 
library(gtable) 
library(grid) 
library(data.table) 
library(scales) 

grid.newpage() 

dt.diamonds <- as.data.table(diamonds) 

d1 <- dt.diamonds[,list(revenue = sum(price), 
         stones = length(price)), 
        by=clarity] 

setkey(d1, clarity) 

圖表

p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill="#4B92DB")) + 
    geom_bar(stat="identity") + 
    labs(x="clarity", y="revenue") + 
    scale_fill_identity(name="", guide="legend", labels=c("Revenue")) + 
    scale_y_continuous(labels=dollar, expand=c(0,0)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1), 
     axis.text.y = element_text(colour="#4B92DB"), 
     legend.position="bottom") 

p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) + 
    geom_point(size=6) + 
    labs(x="", y="number of stones") + expand_limits(y=0) + 
    scale_y_continuous(labels=comma, expand=c(0,0)) + 
    scale_colour_manual(name = '',values =c("red","green"), labels = c("Number of Stones"))+ 
    theme(axis.text.y = element_text(colour = "red")) + 
    theme(panel.background = element_rect(fill = NA), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.border = element_rect(fill=NA,colour="grey50"), 
     legend.position="bottom") 

# extract gtable 
g1 <- ggplot_gtable(ggplot_build(p1)) 
g2 <- ggplot_gtable(ggplot_build(p2)) 


pp <- c(subset(g1$layout, name == "panel", se = t:r)) 
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
        pp$l, pp$b, pp$l) 
# axis tweaks 
ia <- which(g2$layout$name == "axis-l") 
ga <- g2$grobs[[ia]] 
ax <- ga$children[[2]] 
ax$widths <- rev(ax$widths) 
ax$grobs <- rev(ax$grobs) 
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm") 
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1) 
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b) 
# draw it 
grid.draw(g) 

問題:沒有人對如何獲得傳說展示的第二部分的一些技巧?

以下是按照p1,p2,p1 & p2的順序生成的圖表,您會注意到p2的圖例未顯示在組合圖表中。

P1

p1

P2

p2

組合P1 & P2

combined p1 & p2

回答

6

與上面使用的技巧類似,您可以提取圖例,綁定它們,然後用它們覆蓋圖例。

所以從# draw it在你的代碼

# extract legend 
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]] 
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]] 

g$grobs[[which(g$layout$name == "guide-box")]] <- 
            gtable:::cbind_gtable(leg1, leg2, "first") 
grid.draw(g) 

enter image description here

+0

這一工程的絕對的治療開始。如果我想擴展它包含在像我的問題[這裏](http://stackoverflow.com/questions/26728852/create-function-to-ggplot-with-data-table)函數如何強制我的顏色設置在'aes'之外的傳說?或者也可以將單個顏色或data.table列傳遞給'aes_string'來設置填充? – Dan 2014-11-04 19:57:47

+0

我現在發現了一個黑客解決方案。由於'aes_string'似乎需要字符串並將它們轉換爲變量名稱,所以我已將我的紅色轉義爲'「\」red \「」',並且它似乎工作正常。 – Dan 2014-11-04 23:38:11

+0

你似乎對grob十分熟悉,我想知道如何擴展它以允許方面呢?我應該問這是一個新問題嗎? – Dan 2014-11-13 11:35:03