1

I am interested in creating one overall graph that contains both a bar graph and line graph, with each on separate Y axes. For example, I have weather data containing both historic (1990-2013) and current (2014) data for precipitation and temperature at my study site. I would like to plot the historic vs. current precipitation data as a bar graph, and then also plot historic vs. current temperature data as a line graph, all within the same graph. The precipitation Y axis ranges from 0-150, while the temperature y axis ranges from 0-20. I have been able to plot the first graph (precipitation bar graph) with no problem. I am at a lose as to how I would then graph the temperature line graph over the bar graph? Any help would be much appreciated. Below is the code for my bar graph.

precip.bar <- ggplot(data=weather, aes(x=Date, y=Precip, fill=Period))+
geom_bar(position="dodge", colour="black", stat="identity", width=1)+
scale_fill_manual(values=c("#000000", "#D3D3D3"))+
xlab("Date") + ylab("Precipitation (mm)") +
ggtitle("Slug Creek SNOWTEL Data") +
theme_bw()
precip.bar
  • 3
    It's not possible to have a graph with two y-axes in ggplot2. – erc Dec 28 '14 at 18:36
  • See http://stackoverflow.com/a/3101876/3521006 – talat Dec 28 '14 at 19:15
  • If the link from @docendo doesnt convince you to avoid this, it can be done in [lattice](http://www.statmethods.net/advgraphs/trellis.html), which has some nice features with factors and aesthetically pleasing plots. – Minnow Dec 28 '14 at 19:27
  • It is possible with a little work - the example [here](https://rpubs.com/kohske/dual_axis_in_ggplot2) and [this one](http://stackoverflow.com/questions/22701606/how-to-plot-bars-and-one-line-on-two-y-axes-in-the-same-chart-with-r-ggplot/22734987#comment42005315_22734987) based on first example might help. – user20650 Dec 28 '14 at 19:29
  • @RomanLustrik; I don't think it is quite right to add the linked question as a duplicate. It is the same question but it does not provide a solution, says that it is not possible, and instead gives statistical opinion on dual axis merits, (and offers alternatives). There are many posts on SO that show (all apparently based on Kohskes initial Rpubs code) that this is possible. – user20650 Dec 29 '14 at 15:31

0 Answers0