I am trying to overlay a bar chart and a point/line graph in ggplot2. Below I provide some sample data. First I needed to look at counts of lengths, which were a continuous variable, so I had to bin lengths to generate a meaninful frequency distribution (lngth.freq below). I also needed to estimate longevity or survival over various lengths from modeled results so I could look at longevity over the frequency of lenghts. For this I modeled longevity at the length bin midpoints (all.lngth below). Now I want to overlay the two graphics so that the point estimates line up with the bins. I generated the graphics separately (because that seemed more appropriate then trying to do it all in one step based on other posts I've read). Ultimately I should use the bin categories as the x-axis labels for both, but I can't seem to get the two graphics to line up correctly due to the scale differences. lngth is numeric, but the bins are factors. I searched under a variety of key words but can't come up with anything precisely analogous.
First I generated the graphics (this is a subset of the data which is actually much more complex than this):
grp <- as.factor(c("SM","SS","SM","SS","SM","SS","SM","SS","SM","SS","SM","SS","SS","SS","SS"))
lngth <- as.numeric(c(12.5, 12.5, 37.5, 37.5, 62.5, 62.5, 87.5, 87.5, 112.5, 112.5, 137.5, 137.5, 162.5, 187.5, 212.5))
SR <- as.numeric(c(0.99, 0.95, 0.99, 0.97, 0.98, 0.98, 0.97, 0.97, 0.93, 0.95, 0.91, 0.94, 0.93, 0.93, 0.92))
datFreq <- data.frame(grp, lngth, SR)
require(ggplot2)
pd <- position_dodge(5)
all.lngth <- ggplot(data=datFreq, aes(x=lngth, y=SR, color=grp, ymin=0.9, ymax=1.0)) +
geom_point(group=grp, size=3, position=pd) +
geom_line(aes(group=grp), size=1, position=pd) +
labs(title="Mean SR by Length") +
theme(axis.title.x = element_blank())
all.lngth
#
grp2 <- as.factor(rep(c("SM","SS"), 9))
lngthbins <- rep(c("<=25","26-50","51-75","76-100","101-125","126-150","151-175","176-200",">201"), each=2)
lngthbins <- factor(lngthbins, levels=c("<=25","26-50","51-75","76-100","101-125","126-150","151-175","176-200",">201"), ordered=TRUE)
count <- as.integer(c(4,1,7,0,12,4,5,9,1,15,2,10,0,3,0,2,0,1))
datLngth <- data.frame(grp2, lngthbins, count)
attach(datLngth)
lngth.freq <- ggplot(data=datLngth, aes(x=lngthbins, y=count, fill = grp2)) +
geom_bar(position=position_dodge(), stat="identity") +
xlab("Length bins") + ylab("Count")
lngth.freq
I originally wanted a single graphic with the frequency distributions below and a second y-axis for the survival plots above. Some reading on Stackoverflow suggested using grid.arrange() instead and just stacking them, which would work as well I suppose if I can get them aligned.
grid.arrange(all.lngth, lngth.freq)
Sorry for the post length, and any duplication of question.