[R] how to put barchart and line chart in the same plot in ggplot2

John Kane jrkrideau at inbox.com
Sun Aug 5 16:36:08 CEST 2012


As far as I understand ggplot2, you cannot do it.  ggplot2 is pretty much designed to NOT allow two different sets of data  with different y axes in the same plot.

Doing this is generally considered very bad practice.  I'd suggest looking into perhaps using a 2X1 or X2  grid and plotting the two sets perhaps beside or above/below 

Have a look at http://stackoverflow.com/questions/9490482/combined-plot-of-ggplot2-not-in-a-single-plot-using-par-or-layout-functio
#  see http://stackoverflow.com/questions/8615530/place-title-of-multiplot-panel-with-ggplot2  for an example
Kingston ON Canada


> -----Original Message-----
> From: xinwei at stat.psu.edu
> Sent: Sat, 4 Aug 2012 17:17:58 -0700 (PDT)
> To: r-help at r-project.org
> Subject: [R] how to put barchart and line chart in the same plot in
> ggplot2
> 
> dear userR:
> I am trying to plot two dependent variables in the same plot in ggplot2.
> because these two variables have very different magnitude, I have to use
> a
> second Y axis. I hope one variable to be line and the other to be
> barchart.
> The x axis is continuous. Yet since I have to make barchart, I guess I
> have
> to treat it as discrete or categorical.
> I have been google searching for the whole afternoon but do not have any
> clue.
> Can anyone give me a direction (not have to be a complete answer...)?
> 
> many thanks
> 
> 
> 
> --
> View this message in context:
> http://r.789695.n4.nabble.com/how-to-put-barchart-and-line-chart-in-the-same-plot-in-ggplot2-tp4639194.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!



More information about the R-help mailing list