2013-07-18 11 views
7

ho tracciare un grafico 2 geom_point con il seguente codice:Due geom_points aggiungere una legenda

source("http://www.openintro.org/stat/data/arbuthnot.R") 
library(ggplot2) 
ggplot() + 
    geom_point(aes(x = year,y = boys),data=arbuthnot,colour = '#3399ff') + 
    geom_point(aes(x = year,y = girls),data=arbuthnot,shape = 17,colour = '#ff00ff') + 
    xlab(label = 'Year') + 
    ylab(label = 'Rate') 

Voglio semplicemente sapere come aggiungere una leggenda sul lato destro. Con la stessa forma e colore. Il triangolo rosa dovrebbe avere la leggenda "donna" e il cerchio blu la leggenda "uomini". Sembra abbastanza semplice, ma dopo molte prove non ho potuto farlo. (Sono un principiante con ggplot).

enter image description here

risposta

11

Se si rinomina le colonne della cornice di dati originale e poi fonderlo in formato lungo con reshape2::melt, è molto più facile da gestire in ggplot2. Specificando l'estetica color e shape nel comando ggplot e specificando manualmente le scale per i colori e le forme, verrà visualizzata la legenda.

source("http://www.openintro.org/stat/data/arbuthnot.R") 
library(ggplot2) 
library(reshape2) 

names(arbuthnot) <- c("Year", "Men", "Women") 

arbuthnot.melt <- melt(arbuthnot, id.vars = 'Year', variable.name = 'Sex', 
    value.name = 'Rate') 

ggplot(arbuthnot.melt, aes(x = Year, y = Rate, shape = Sex, color = Sex))+ 
geom_point() + scale_color_manual(values = c("Women" = '#ff00ff','Men' = '#3399ff')) + 
scale_shape_manual(values = c('Women' = 17, 'Men' = 16)) 

enter image description here

+0

grazie è esattamente quello che stavo cercando. La struttura sembra essere leggermente più difficile da ottenere rispetto all'utilizzo solo di ggplot2. Daremo un'occhiata alla documentazione di reshape2 ... Grazie – S12000

3

Ecco un modo di fare questo senza usare rimodellare :: sciogliersi. reshape :: melt funziona, ma puoi entrare in un vincolo se vuoi aggiungere altre cose al grafico, come i segmenti di linea. Il codice seguente utilizza l'organizzazione originale dei dati. La chiave per modificare la legenda è assicurarsi che gli argomenti su scale_color_manual (...) e scale_shape_manual (...) siano identici altrimenti otterrete due legende.

source("http://www.openintro.org/stat/data/arbuthnot.R") 
library(ggplot2) 
library(reshape2) 



ptheme <- theme (
    axis.text   = element_text(size = 9),    # tick labels 
    axis.title   = element_text(size = 9),    # axis labels 
    axis.ticks   = element_line(colour = "grey70", size = 0.25), 
    panel.background  = element_rect(fill = "white", colour = NA), 
    panel.border   = element_rect(fill = NA, colour = "grey70", size = 0.25), 
    panel.grid.major  = element_line(colour = "grey85", size = 0.25), 
    panel.grid.minor  = element_line(colour = "grey93", size = 0.125), 
    panel.margin   = unit(0 , "lines"), 
    legend.justification = c(1, 0), 
    legend.position  = c(1, 0.1), 
    legend.text   = element_text(size = 8), 
    plot.margin   = unit(c(0.1, 0.1, 0.1, 0.01), "npc") # c(bottom, left, top, right), values can be negative 
) 

cols <- c("c1" = "#ff00ff", "c2" = "#3399ff") 
shapes <- c("s1" = 16, "s2" = 17) 

p1 <- ggplot(data = arbuthnot, aes(x = year)) 
p1 <- p1 + geom_point(aes(y = boys, color = "c1", shape = "s1")) 
p1 <- p1 + geom_point(aes(y = girls, color = "c2", shape = "s2")) 
p1 <- p1 + labs(x = "Year", y = "Rate") 
p1 <- p1 + scale_color_manual(name = "Sex", 
           breaks = c("c1", "c2"), 
           values = cols, 
           labels = c("boys", "girls")) 
p1 <- p1 + scale_shape_manual(name = "Sex", 
           breaks = c("s1", "s2"), 
           values = shapes, 
           labels = c("boys", "girls")) 
p1 <- p1 + ptheme 

print(p1) 

output results

+0

Questo codice diventerà piuttosto complicato se abbiamo, ad esempio, 5-6 + categorie di frutta, anziché "ragazzi" e "ragazze". – zx8754

Problemi correlati