R graph with two y-axes

I’ve been asked how to do this several times, so I thought it might help to put an example 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:


  • Logan

    Thank you very much for posting this code. It was exactly what I needed.

  • Duleep Kumar Samuel

    Thank you so much, lovely graph

  • Julius Sumihar

    Hey, thanks a lot for making this example online. It has helped me! Thanks!!

  • Beverly Schnell

    Thank you very much.
    This was very helpful.

  • kmelis

    Those are not axes, those are just lines.
    It is helpful though, for plotting different plots on top of eachother.

    • Long

      he means two different y axes with different measurement

  • basem

    thank you very much; it was very helpful for me

  • Tim

    Thanks,
    what I was looking for!

  • ola

    Very helpful.

  • Khaya

    Very helpful

  • ana

    Thanks, very helpful for me too

  • mario

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

    • My example does not involve time series, so I don’t know what you mean. If you change the x values in the second plot, then you will won’t get anything useful. 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

  • Barbarahcosta

    I tried to repeat your example but y2 ylab for axis 4 is horizontal, and I’m trying to find out how to turn it vertical as you showed in this example. What is it missing?
    Thanks a lot
    Barbara

    • You probably have your R options set to make labels horizontal. What I have shown is default behaviour in R.

  • Rashik

    Thank you a lot. You saved my life!

  • Carla

    Thanks, this really helped me alot, i spent some time looking for a feature like this.

  • 吉翔骏

    xiexieni

  • Thanks. Very helpful.

  • flop

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

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

    • Try reading the help files. It is setting margins around the plot.

  • TG

    Thanks for this helpful code! It works fine but for the labeling of the right y axis.

    I am using the following command lines:

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

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

    The labeling on the left shows up fine. On the left no labeling shows up but there is also no error message. If I leave the “line=” command out it shows up right next to the axis but with “line=3” No labeling shows up at all.
    Any suggestions?
    Thanks!

    • Increase your margins, or use a smaller value for line.

      • TG

        works! Thanks!

  • zzz

    Could you tell me how to set the starting points coincide? Thanks in advance

    • I’ve no idea what you mean. Starting points of what?

      • zzz

        I need the 2 plots starting from the same point for comparing purpose. Do you know how to do that?

        • zzz

          In your example, either shift up the red plot or shift down the blue plot.
          I’m not an English speaker. Hope you know what I mean. Thanks

          • OK. I understand what you mean now. But I don’t know how to do it. If the first points are both mins or maxs it will happen anyway. Otherwise, you will have to fiddle with the y-axis until it works.

          • Selva Prabhakaran

            Try setting the ‘ylim’ parameter for each line to start from a value ‘x’ where x equals the first value in the series minus (the greater of (the difference of the first value and minimum most value in either of the series)).

  • Long

    Thanks. But how can we give the two y axes the same scale? They are not of same scale in this example.

    • If they are on the same scale, there is nothing to do. Just use axis().

  • Surah

    But if I want to use plot.xts() to draw two time series into one plot, how can I add an additional y-axis? Because the xts package does not support all the normal plot functions…i.e. axis(), Thx!

  • Pingback: R graph with two y-​​axes | funature blog()

  • Hamster

    thank you very much =)

  • Selva Prabhakaran

    Is there a way to make my x’axis to run at midpoint of y’axis ? so that my line will cross over below and above the x axis ?

  • hans

    thanks!!!!

  • Paul Brennan

    I know you did this a long time ago but I just found it and it’s very helpful.
    Thanks, P

  • Ratnanil

    Thanks so much! One question: i’d like both axes to have the same 0 point.. how do I manage this?

  • Victor Mayrink

    Thank you so much, it was very helpful! Is there any way to do this using ggplot2?

  • Adebayo Oni

    please what is the code for multivariate weibull distribution

  • Gerard Blais

    Thanks! Just what I needed.

  • Name

    how about for barplot? how to have different y-axis? one on the right, one on the left.

  • blink_me

    How does one achieve this with ggplot2? Thanks in advance!

  • Clem

    Thank you, it works!