0

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.

A.Birdman
  • 161
  • 1
  • 2
  • 12
  • There are a few links in [this question](http://stackoverflow.com/questions/24709307/keep-all-plot-components-same-size-in-ggplot2-between-two-plots) which look like they will help you; like the [align_plots](https://gist.github.com/baptiste/2877821) function, for example. – MrFlick Aug 08 '14 at 15:27
  • Thanks for the quick response. I've been digging into the question you linked with several promissing leads, but continue to be stopped because my structure doesn't seem to fit easily into any of those bins. I looked at your link for align_plots, but it is just code with no context that I can see, and I am not that savvy a user to figure out how to make all the needed changes to get it to work. I'm continuing to try setting up other data structures, etc., but if other users have suggestions, I'd love to hear them. – A.Birdman Aug 09 '14 at 01:49
  • Using your plots above: `align_plots(all.lngth, lngth.freq, width=unit(5, "in"))` should you. You may want to play with the `width=` setting to get your plot how you like. – MrFlick Aug 09 '14 at 02:20
  • Thanks. You are a scholar and a gentleman. I just want to get this analysis off to my collaborator in short order, and I keep getting stuck in the minutia. Again, thanks much. – A.Birdman Aug 09 '14 at 19:52

0 Answers0