2012-04-17 29 views
5

Edificio sul mio previous question Vorrei aggiungere un'etichetta del secondo asse sul lato opposto del grafico.Come posso aggiungere etichette del secondo asse in ggplot2?

La cornice di dati si presenta come:

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)) 

Utilizzando Tyler's solution, un grafico di sembra che al momento:

enter image description here

In modo simile ad una trama foresta I' Mi piacerebbe aggiungere un secondo set di etichette (variabile label nella mia cornice dati) che rappresenta valori grafici, preferibilmente sul lato destro dei pannelli. In modo che tutto trama imita foresta simile a questo esempio:

enter image description here

So che secondo asse seems to be frowned upon. Tuttavia questi sono solo un altro insieme di etichette .. e sembra essere un'abitudine tra i lotti di foresta.

Come posso farlo in ggplot?

+1

le risposte qui saranno forse vi darà un punto di partenza: http://stackoverflow.com/questions/4867597/can-you-easily- plot-rugs-axes-on-the-top-right-in-ggplot2/4868130 # 4868130 – Chase

+0

@Chase: Grazie per quello. Infatti molto facilmente hackerabile usando 'trama'. Ti stai ancora chiedendo se è possibile farlo in ggplot2? – radek

+1

Il secondo asse viene sfigurato solo quando è una variabile totalmente diversa (ad esempio temperatura e concentrazione di sedimento). Un secondo asse che è una versione trasformata del primo è perfettamente accettabile (ad esempio la temperatura in C, o in K, o in F). –

risposta

4

EDIT l'aggiornamento a 0.9.3 ggplot2

Aggiungendo il set di etichette nel vostro dataframe test per la tabella sfaccettato è semplice. Utilizzare geom_text con l'estetica per le etichette e le posizioni xey delle etichette. Nel codice sottostante, xlim crea un po 'più di spazio per le etichette. Il seguente codice:

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)) 

produce:

enter image description here

+0

Fantastico. Molte grazie per l'aiuto! – radek

Problemi correlati