A blog by Rob J Hyndman 

Twitter Gplus RSS

R graph with two y-​​axes

Published on 5 February 2008

I’ve been asked how to do this sev­eral times, so I thought it might help to put an exam­ple online.

x <- 1:5
y1 <- rnorm(5)
y2 <- rnorm(5,20)
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red")
par(new=TRUE)
plot(x, y2,,type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
mtext("y2",side=4,line=3)
legend("topleft",col=c("red","blue"),lty=1,legend=c("y1","y2"))



Related Posts:


 
Tags:
33 Comments  comments 
  • Logan

    Thank you very much for post­ing this code. It was exactly what I needed.

  • Duleep Kumar Samuel

    Thank you so much, lovely graph

  • Julius Sum­i­har

    Hey, thanks a lot for mak­ing this exam­ple online. It has helped me! Thanks!!

  • Bev­erly Schnell

    Thank you very much.
    This was very helpful.

  • http://flyordie.sin.khk.be kmelis

    Those are not axes, those are just lines.
    It is help­ful though, for plot­ting dif­fer­ent plots on top of eachother.

    • Long

      he means two dif­fer­ent y axes with dif­fer­ent measurement

  • basem

    thank you very much; it was very help­ful for me

  • Tim

    Thanks,
    what I was look­ing for!

  • ola

    Very help­ful.

  • Khaya

    Very help­ful

  • ana

    Thanks, very help­ful for me too

  • mario

    what if x1=1:5 and x2=3:7? how to plot this? when I try the same exam­ple above, then R fits both time-​​series on top of each other rather than lagged..

    • http://robjhyndman.com Rob J Hyndman

      My exam­ple does not involve time series, so I don’t know what you mean. If you change the x val­ues in the sec­ond plot, then you will won’t get any­thing use­ful. If you want to plot time series, than make ts objects first.

    • Isaac Miyakawa

      To Mario:
      Use xlim 1 to 7 in each plot().

  • Shon

    Thank you so helpful..

  • Guest

    Great! Thanx.

  • RM

    Thanks a lot. This is very useful

  • Bar­barah­costa

    I tried to repeat your exam­ple but y2 ylab for axis 4 is hor­i­zon­tal, and I’m try­ing to find out how to turn it ver­ti­cal as you showed in this exam­ple. What is it miss­ing?
    Thanks a lot
    Barbara

    • http://robjhyndman.com Rob J Hyndman

      You prob­a­bly have your R options set to make labels hor­i­zon­tal. What I have shown is default behav­iour in R.

  • Rashik

    Thank you a lot. You saved my life!

  • Carla

    Thanks, this really helped me alot, i spent some time look­ing for a fea­ture like this.

  • 吉翔骏

    xiex­ieni

  • http://twitter.com/polarise polarise

    Thanks. Very helpful.

  • flop

    what does “mar” means?
    and what is the line ”

    par(mar=c(5,4,4,5)+.1)” doing?

    • http://robjhyndman.com Rob J Hyndman

      Try read­ing the help files. It is set­ting mar­gins around the plot.

  • TG

    Thanks for this help­ful code! It works fine but for the label­ing of the right y axis.

    I am using the fol­low­ing com­mand lines:

    > mtext(“SpO2, PR, HALO”, side=4, line=3)

    > mtext(“SpHb, RRa, PI”, side=2, line=3)

    The label­ing on the left shows up fine. On the left no label­ing shows up but there is also no error mes­sage. If I leave the “line=” com­mand out it shows up right next to the axis but with “line=3″ No label­ing shows up at all.
    Any sug­ges­tions?
    Thanks!

    • http://robjhyndman.com Rob J Hyndman

      Increase your mar­gins, or use a smaller value for line.

      • TG

        works! Thanks!

  • zzz

    Could you tell me how to set the start­ing points coin­cide? Thanks in advance

    • http://robjhyndman.com/ Rob J Hyndman

      I’ve no idea what you mean. Start­ing points of what?

      • zzz

        I need the 2 plots start­ing from the same point for com­par­ing pur­pose. Do you know how to do that?

        • zzz

          In your exam­ple, either shift up the red plot or shift down the blue plot.
          I’m not an Eng­lish speaker. Hope you know what I mean. Thanks

          • http://robjhyndman.com/ Rob J Hyndman

            OK. I under­stand what you mean now. But I don’t know how to do it. If the first points are both mins or maxs it will hap­pen any­way. Oth­er­wise, you will have to fid­dle with the y-​​axis until it works.