[R] How to get two y-axises in a bar plot?

hadley wickham h.wickham at gmail.com
Tue Jan 29 18:11:28 CET 2008

On Jan 29, 2008 10:15 AM, Gustaf Granath <Gustaf.Granath at ebc.uu.se> wrote:
> Hi,
> I have measured two response variables (y1, y2) at each treatment level
> (x = 0, 1.5 or 3). Now I would like to show the y1 and y2 against x in a
> bar plot. However, y1 and y2 differ in scale so I need two y-axises, one
> on the left side and one on the right side (and I dont want to
> standardize my responses). This is fairly easy if you want to show
> points,lines etc, but gets more complicated with bars.Although these
> kind of bar graphs are quite common, I have found very limited
> information about how to do them in R. I have been struggling with the
> barplot() command. My problem is that the bars for y1 and y2 end up at
> the same place (blocking each other) and not beside each other when I
> use par(new=TRUE). Is there a way to separate them so y1 and y2 are
> placed beside each other at each x level, or is this easier to do this
> with lattice?? I would also like to add error bars but I guess that
> should not be a problem.

Why don't you just use two separate plots?  Using double y-axes is
only desirable if one axis is a transformation of the other, or you
are deliberately trying to confuse the data.

If you want to learn more about why it's a bad idea, trying start
with: K. W. Haemer. Double scales are dangerous. The American
Statistician, 2(3):24–24, 1948.  You'll note that people have been
advising against them for over 50 years!



More information about the R-help mailing list