Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:11:57+00:00 2026-05-31T04:11:57+00:00

This is probably not a duplicate as I am not requesting how to plot

  • 0

This is probably not a duplicate as I am not requesting how to plot 2 series in the same plot, but rather how to add 2 (interconnected) y scales on one XY series

Here is the frame

t <- data.frame(x=seq(0,1,0.01), y=exp(seq(0,1,0.01))*500, y2=exp(seq(0,1,0.01))*30)

head(t)
     x        y       y2
1 0.00 500.0000 30.00000
2 0.01 505.0251 30.30151
3 0.02 510.1007 30.60604
4 0.03 515.2273 30.91364
5 0.04 520.4054 31.22432
6 0.05 525.6355 31.53813

y and y2 are linearly interconnected. I would like to construct a plot with x, y (at left, side 2) and y2 (at right, side 4) so that each point on the plot has one x and 2 y coordinates.

base R only solutions please.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:11:59+00:00Added an answer on May 31, 2026 at 4:11 am

    From what I understand, you want y and y2 to correspond exactly (in terms of the scales)?

    In that case, you can plot y vs x which draws an axis on the left hand side, and then draw an identical axis on the right hand side, except that you change the labels.

    In base graphics:

    # do first plot. Don't draw axis (we'll do it later)
    plot(y~x,data=t,axes=F,ylab='y')
    # draw y axis (see ?axis, 1=bottom, 2=left, 3=top, 4=right)
    axis(2,pretty(range(t$y)))
    
    # tell R to draw over the first plot
    par(new=T)
    
    # do second plot. 
    plot(y2~x,data=t,axes=F,ylab="")
    # draw second axis on the right
    axis(4,pretty(range(t$y2)),ylab='y2')
    
    # draw x axis on the bottom
    axis(1,pretty(range(t$x)))
    
    # draw the box if you want
    box()
    

    How this works:

    1) plot y vs x. Doesn’t draw the axes, because:
    2) we draw on the axes. axis(2,pretty(range(t$y))). pretty takes a start and end point, and generates suitable axis tick marks (usually multiples of 5 or 10)
    3) par(new=T) : we tell R that the next plot it does should be drawn over the top of the current – don’t wipe away what we’ve drawn so far!
    4) we plot y2 vs x. This resets the coordinate system to the y2 coordinate system.
    5) we draw the right hand axis. This works because since we’ve just plotted y2 vs x, the coordinate system is for y2 (and not for y as it was when we first started plotting).
    6) draw on the x axis
    7) draw the box around the plot (if you like).

    Tweaks

    You may notice that the plot is lop-sided – there’s a lot more white space between the left hand side of the plot & the edge of the graphics device than there is on the right. That’s because R wants to make space for the y label which is usually drawn on the left.

    If you want to even it up, use par(mar=c(top,left,bottom,right)).

    Looking at ?par, we see the default is c(5,4,4,2)+.1.

    So, add this snippet to the front of your code:

    # get the current margins (top, left, bottom, right)
    m <- par('mar')
    # make sure the right margin is the same as the left.
    m[4]<-m[2]
    # set the new margins
    par(mar=m)
    
    # .... perform plotting as above.
    

    Now you can see that there’s equal space either side of the plot, so ti doesn’t look lopsided any more. However, you may also notice that there is a y label on the left axis but not on the right.

    It’s a bit ugly – we have to add it in manually using mtext (which draws text on the plot):

    mtext('y2',4,line=2)
    

    The 'y2' is the y axis label, the 4 means “draw on the right hand side of the plot”, and line=2 says “draw the label on line 2, starting from 0 at the axis and counting outwards”.

    Summary

    # margin
    m <- par('mar')
    m[4] <- m[2]
    par(mar=m)
    
    # plotting
    plot(y~x,data=t,axes=F,ylab='y')
    axis(2,pretty(range(t$y)))
    par(new=T)
    plot(y2~x,data=t,axes=F,ylab="")
    axis(4,pretty(range(t$y2)),ylab='y2')
    axis(1,pretty(range(t$x)))
    box()
    
    # right-hand ylabel
    mtext('y2',4,line=2)
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This is probably a duplicate, but I could not find it - feel free
Possible Duplicate: Regex to parse youtube yid your probably thinking not another one, but
This is weird and probably not possible but I'll ask anyway. I'm making this
I know this probably really simple but Im not sure what im doing wrong...
I'm sure there's a clean way to do this, but I'm probably not using
The answer to this is probably right under my nose, but I am not
Sorry this is probably super basic. But in all my javabean examples, I've not
I realize this question has probably been asked numerous times, but I have not
This is probably a duplicate to many but the obvious answers in them do
So, here's the problem (it's probably an easy one :P) This is my table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.