2012-07-27 14 views
6

¿Es posible tener dos leyendas con ggplot2 pero basadas en diferentes conjuntos de datos? Por ejemplo, en el siguiente código me gustaría obtener la leyenda de la primera situación y la leyenda de la segunda situación en el mismo gráfico. Mi intento (tercera situación) no funciona.dos leyendas basadas en diferentes conjuntos de datos con ggplot2

library(ggplot2) 
library(scales) 

yrng <- range(economics$unemploy) 
xrng <- range(economics$date) 
presidential <- presidential[-(1:3), ] 

# add a fictive factor to the economics dataset 
economics <- cbind.data.frame(economics, col=gl(2, nrow(economics)/2)) 

##################### 
## first situation ## 
##################### 
# first plot with legend 
unemp <- qplot(date, unemploy, data=economics, geom="line", 
       xlab = "", ylab = "No. unemployed (1000s)", colour=col) 
# second plot without legend 
unemp + geom_vline(aes(xintercept = start), data = presidential) 

###################### 
## second situation ## 
###################### 
# first plot without legend 
unemp <- qplot(date, unemploy, data=economics, geom="line", 
       xlab = "", ylab = "No. unemployed (1000s)") 
# second plot with legend 
unemp + 
    geom_rect(aes(NULL, NULL, xmin = start, xmax = end, 
        fill = party), ymin = yrng[1], ymax = yrng[2], 
        data = presidential) + 
    scale_fill_manual(values = alpha(c("blue", "red"), 0.2)) 


##################### 
## third situation ## 
##################### 
# first plot with legend 
unemp <- qplot(date, unemploy, data=economics, geom="line", 
       xlab = "", ylab = "No. unemployed (1000s)", colour=col) 
# second plot with legend 
unemp + 
    geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party), ymin = yrng[1], 
       ymax = yrng[2], data = presidential) + 
    scale_fill_manual(values = alpha(c("blue", "red"), 0.2)) 

Error in data.frame(xmin = 11342, xmax = 14264, fill = "Republican", colour = function (x, : 
    arguments imply differing number of rows: 1, 0 
+1

El código debería ser idealmente fácil de cortar y pegar en R. Como tiene actualmente, los caracteres '>' y '+' que no lo hacen posible. Es difícil ver lo que estás haciendo como resultado. –

+0

@ mindless.panda Esto no es un problema: copie el código y luego haga "Pegar solo comandos" en la consola R :-) –

+0

Está asumiendo que todos usan la consola R estándar. =) –

Respuesta

13

En general, una vez que comienza a recibir a las parcelas más complicadas, es casi siempre mejor que deje de usar qplot y utilizar ggplot lugar. Me resulta más fácil pensar en la forma en que estoy construyendo la trama pieza por pieza:

ggplot() + 
    geom_line(aes(x=date, y=unemploy, color=col), data=economics) + 
    geom_rect(aes(xmin=start, xmax=end, fill=party), 
      ymin = yrng[1], ymax = yrng[2], data = presidential) + 
    scale_fill_manual(values = alpha(c("blue", "red"), 0.2)) +   
    xlab("") + 
    ylab("No. unemployed (1000s)") 

Esto da:

plot

Cuestiones relacionadas