[R] ggplot2: further query about back to back bar plots
Gavin Rudge
g.rudge at bham.ac.uk
Thu Jul 25 19:30:32 CEST 2013
Further to my recent post on this topic and thanks to help received already (thanks BTW), I've got back-to-back plots working nicely to give me population pyramids, with some overlaid point data from a different time period, using the code below.
#packages
library(ggplot2)
library(reshape2)
library(plyr)
#sample data
set.seed(33)
df<-data.frame(ag=c(1:18),males_year1=sample(100:200,18),females_year1=sample(100:200,18),males_year2=sample(100:200,18),females_year2=sample(100:200,18))
#melt the data set
df<-data.frame(melt(df,id="ag"))
df
#here is the plot
p<-ggplot(df)+
geom_bar(subset=.(df$variable=="males_year1"),stat="identity",aes(x=ag,y=value),fill="#6666FF")+
geom_bar(subset=.(df$variable=="females_year1"),stat="identity",aes(x=ag,y=-value),fill="#FF9333")+
geom_point(subset=.(df$variable=="males_year2"),stat="identity",aes(x=ag,y=value),size=3,colour="#330099")+
geom_point(subset=.(df$variable=="females_year2"),stat="identity",aes(x=ag,y=-value),size=3,colour="#CC3300")+
coord_flip()+
theme_bw()+
scale_y_continuous(limits=c(-200,200),breaks=seq(-200,200,50),labels=abs(seq(-200,200,50)))+
scale_x_continuous(limits=c(0,19),breaks=seq(1,18,1),labels=abs(seq(1,18,1)))+
xlab("age group")+ylab("population")+
theme_bw()+
xlab("age group")+
ylab("population")+
geom_text(y=-100,x=19.2,label="Females")+
geom_text(y=100,x=19.2,label="Males")
p
Two questions remaining. Firstly have I used a large amount of code to acheive this or is this about right for the effect that I'm after?
Secondly I'm quite confused about how to put a legend onto a plot like this. I'm getting slowly into the ggplot way of doing things, but I'm totally baffled by legends; say I wanted a legend with an appropriate label for both genders and both time periods showing the colours of the bars and dots I've used here as examples, how do I do this? I've tried scale_fill with a bunch of arguments to no avial. I'm confused about where in the hierarchy of ggplot commands you actually build the legend and how you map it to your data. The usual trawl of the package pdf / cook book for R etc hasn't really helped. Can someone show me how to do this please?
Many thanks.
Gavin.
More information about the R-help
mailing list