add legend manually

36 views
Skip to first unread message

Alessandra Carioli

unread,
Apr 10, 2014, 11:36:31 AM4/10/14
to ggplot2
Dear All,

I am trying to add manually a legend to a plot, tried to follow examples on forums but I am still missing something.
Any help on the issue is greatly appreciated!

Ale

dati: data.frame
Year: years from 1975 to 2011
Births: number of births
Age_Group: age groups from 1 to 7

p <- ggplot(dati, aes(Year,Births1))
p + geom_line(data=dati[which(dati$Age_Group=="1"),],colour = "red", size = 1) +
geom_line(data=dati[which(spain.agegroup$Age_Group=="2"),],colour = "lightblue", size = 1) +
geom_line(data=dati[which(spain.agegroup$Age_Group=="3"),],colour = "green", size = 1) +
scale_y_continuous(breaks=c(0,25,50,75,100,125,150,175))+
scale_x_continuous(breaks=c(1975,1980,1985,1990,1995,2000,2005,2011),limits=c(1974,2012))+
theme(axis.line=element_line(color="black"),
axis.text= element_text(colour="black", size=20, angle=0,face = "plain"),
panel.background=element_rect(fill="white"),
panel.grid=element_line(colour="grey"),
axis.text.x = element_text(hjust=.5, vjust=.5),
axis.text.y = element_text(hjust=1, vjust=0),
strip.text.x = element_text(size = 8, colour =
"black", angle = 0),
plot.title= element_text(size=20))+
#my line for legend...
scale_colour_manual(name="Age Groups",values=c("15-19"="red","20-24"="lightblue","25-29"="green","30-34"="darkorange","35-39"="brown","40-44"="darkblue","45-49"="violet"))


Alessandra Carioli
NIDI and University of Groningen
Langehoutstraat 19
2511 CV
+31(0)70-3565259
car...@nidi.nl

Alessandra Carioli

unread,
Apr 10, 2014, 11:40:12 AM4/10/14
to ggplot2
the last line would have only three groups, sorry for the mistake!


p <- ggplot(dati, aes(Year,Births1))
p + geom_line(data=dati[which(dati$Age_Group=="1"),],colour = "red", size = 1) +
geom_line(data=dati[which(dati$Age_Group=="2"),],colour = "lightblue", size = 1) +
geom_line(data=dati[which(dati$Age_Group=="3"),],colour = "green", size = 1) +
scale_y_continuous(breaks=c(0,25,50,75,100,125,150,175))+
scale_x_continuous(breaks=c(1975,1980,1985,1990,1995,2000,2005,2011),limits=c(1974,2012))+
theme(axis.line=element_line(color="black”),
axis.text= element_text(colour="black", size=20, angle=0,face = "plain”),
panel.background=element_rect(fill="white”),
panel.grid=element_line(colour="grey”),
axis.text.x = element_text(hjust=.5, vjust=.5),
axis.text.y = element_text(hjust=1, vjust=0),
strip.text.x = element_text(size = 8, colour ="black", angle = 0),
plot.title= element_text(size=20))+
#my line for legend...
scale_colour_manual(name="Age Groups",values=c("15-19"="red","20-24"="lightblue","25-29"=“green”)

Dennis Murphy

unread,
Apr 10, 2014, 1:55:21 PM4/10/14
to Alessandra Carioli, ggplot2
You need to put the statements "colour = value" *inside* aes() for
each of the first three layers you add to p if you want to compute a
factor on the fly in ggplot().

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Alessandra Carioli

unread,
Apr 10, 2014, 2:47:10 PM4/10/14
to Dennis Murphy, ggplot2
Thanks, now it works.
The only thing that seems not to work is legend.key.width (I want a bigger legend) as R does not recognize unit (although I’v seen some examples with it online)

ggplot(spain.agegroup, aes(Year,Births,colour=factor(Age)))+
geom_line(size=1)+
labs(title = "", x = "", y = "") +
theme(axis.line=element_line(color="black"),
axis.text= element_text(colour="black", size=20, angle=0,face = "plain"),
panel.background=element_rect(fill="white"),
panel.grid=element_line(colour="grey"),
axis.text.x = element_text(hjust=.5, vjust=.5),
axis.text.y = element_text(hjust=1, vjust=0),
strip.text.x = element_text(size = 8, colour =
"black", angle = 0),
plot.title= element_text(size=20))+
theme(legend.background =element_rect("white"))+
scale_colour_manual(name = '', values = c("red","lightblue","green","darkorange","brown","darkblue","violet" ))+
theme(legend.key=element_rect("white",colour="white"),
legend.text =element_text(size=20),
legend.key.width=unit(3,"line”)) #here is the error

Dennis Murphy

unread,
Apr 10, 2014, 3:42:05 PM4/10/14
to Alessandra Carioli, ggplot2
unit() is a function in the grid package. Load grid first.

Dennis

On Thu, Apr 10, 2014 at 11:47 AM, Alessandra Carioli
Reply all
Reply to author
Forward
0 new messages