2012-04-17 86 views
5

建立在我的previous question上我想在圖的另一側添加第二個軸標籤。如何在ggplot2中添加第二個軸標籤?

數據幀是這樣的:使用Tyler's solution

test <- structure(list(characteristic = structure(c(1L, 2L, 3L, 1L, 2L 
), .Label = c("Factor1", "Factor2", "Factor3"), class = "factor"), 
    es = c(1.2, 1.4, 1.6, 1.3, 1.5), ci_low = c(1.1, 1.3, 1.5, 
    1.2, 1.4), ci_upp = c(1.3, 1.5, 1.7, 1.4, 1.6), label = structure(c(1L, 
    3L, 5L, 2L, 4L), .Label = c("1.2 (1.1, 1.3)", "1.3 (1.2, 1.4)", 
    "1.4 (1.3, 1.5)", "1.5 (1.4, 1.6)", "1.6 (1.5, 1.7)"), class = "factor"), 
    set = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("H", "S" 
    ), class = "factor")), .Names = c("characteristic", "es", 
"ci_low", "ci_upp", "label", "set"), class = "data.frame", row.names = c(NA, 
-5L)) 

,它的圖形看起來像在時刻:

enter image description here

以類似的方式,以森林情節我d想要添加第二組標籤(我的數據框中的變量爲label),代表繪圖值,最好在面板的右側。因此,它類似於此示例中,所有模仿森林圖:

enter image description here

我知道,第二軸seems to be frowned upon。然而,這些只是另一組標籤..螞蟻似乎是森林地塊中的一種習俗。

我該怎麼做ggplot?

+1

的答案在這裏將可能給你一個起點:http://stackoverflow.com/questions/4867597/can-you-easily- plot-rugs-axes-on-the-top-right-in-ggplot2/4868130#4868130 – Chase 2012-04-17 02:02:54

+0

@Chase:謝謝你。確實很容易使用'plot'來破解。仍然想知道是否可以在ggplot2中做到這一點? – radek 2012-04-18 16:12:04

+1

第二軸只有在完全不同的變量(例如溫度和沉積物濃度)時纔會皺起眉頭。作爲第一軸的變換形式的第二軸是完全可接受的(例如,溫度以℃,或以K,或在F中)。 – 2012-05-04 08:45:24

回答

4

編輯更新到0.9.3 GGPLOT2

添加您的標籤集測試數據幀到多面的圖表很簡單。使用geom_text,標籤的美學和標籤的x和y位置。在下面的代碼中,xlim爲標籤創建了更多空間。下面的代碼:

library(gridExtra) 
library(ggplot2) 

p <- ggplot(test, aes(y = characteristic, x = es, xmin = ci_low, xmax = ci_upp)) + 
    geom_point() + 
    geom_errorbarh(height = 0) + 
    geom_text(aes(label = label, x = 2, y = characteristic)) + 
    scale_x_continuous(limits = c(1, 2.2), breaks = c(1, 1.2, 1.4, 1.6, 1.8), 
    labels=c("1.0", "1.2", "1.4", "1.6", "1.8")) + 
    facet_grid(set ~ ., scales = "free", space = "free") + 
    theme_bw() + 
    theme(strip.text.y = element_text(angle = 0), 
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank()) 
p 

grid.text(expression(paste("ES " %+-% " ci")), x = 0.78, y = .92, 
    gp = gpar(fontsize = 18)) 

生產:

enter image description here

+0

太棒了。非常感謝您的幫助! – radek 2012-04-19 19:52:30