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 5954667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:00:12+00:00 2026-05-22T18:00:12+00:00

I would like superimpose two scatter plots in R so that each set of

  • 0

I would like superimpose two scatter plots in R so that each set of points has its own (different) y-axis (i.e., in positions 2 and 4 on the figure) but the points appear superimposed on the same figure.

Is it possible to do this with plot?

Edit Example code showing the problem

# example code for SO question
y1 <- rnorm(10, 100, 20)
y2 <- rnorm(10, 1, 1)
x <- 1:10
# in this plot y2 is plotted on what is clearly an inappropriate scale
plot(y1 ~ x, ylim = c(-1, 150))
points(y2 ~ x, pch = 2)
  • 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-22T18:00:13+00:00Added an answer on May 22, 2026 at 6:00 pm

    update: Copied material that was on the R wiki at http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes, link now broken: also available from the wayback machine

    Two different y axes on the same plot

    (some material originally by Daniel Rajdl 2006/03/31 15:26)

    Please note that there are very few situations where it is appropriate to use two different scales on the same plot. It is very easy to mislead the viewer of the graphic. Check the following two examples and comments on this issue (example1, example2 from Junk Charts), as well as this article by Stephen Few (which concludes “I certainly cannot conclude, once and for all, that graphs with dual-scaled axes are never useful; only that I cannot think of a situation that warrants them in light of other, better solutions.”) Also see point #4 in this cartoon …

    If you are determined, the basic recipe is to create your first plot, set par(new=TRUE) to prevent R from clearing the graphics device, creating the second plot with axes=FALSE (and setting xlab and ylab to be blank – ann=FALSE should also work) and then using axis(side=4) to add a new axis on the right-hand side, and mtext(...,side=4) to add an axis label on the right-hand side. Here is an example using a little bit of made-up data:

    set.seed(101)
    x <- 1:10
    y <- rnorm(10)
    ## second data set on a very different scale
    z <- runif(10, min=1000, max=10000) 
    par(mar = c(5, 4, 4, 4) + 0.3)  # Leave space for z axis
    plot(x, y) # first plot
    par(new = TRUE)
    plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
    axis(side=4, at = pretty(range(z)))
    mtext("z", side=4, line=3)
    

    twoord.plot() in the plotrix package automates this process, as does doubleYScale() in the latticeExtra package.

    Another example (adapted from an R mailing list post by Robert W. Baer):

    ## set up some fake test data
    time <- seq(0,72,12)
    betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35)
    cell.density <- c(0,1000,2000,3000,4000,5000,6000)
    
    ## add extra space to right margin of plot within frame
    par(mar=c(5, 4, 4, 6) + 0.1)
    
    ## Plot first set of data and draw its axis
    plot(time, betagal.abs, pch=16, axes=FALSE, ylim=c(0,1), xlab="", ylab="", 
       type="b",col="black", main="Mike's test data")
    axis(2, ylim=c(0,1),col="black",las=1)  ## las=1 makes horizontal labels
    mtext("Beta Gal Absorbance",side=2,line=2.5)
    box()
    
    ## Allow a second plot on the same graph
    par(new=TRUE)
    
    ## Plot the second plot and put axis scale on right
    plot(time, cell.density, pch=15,  xlab="", ylab="", ylim=c(0,7000), 
        axes=FALSE, type="b", col="red")
    ## a little farther out (line=4) to make room for labels
    mtext("Cell Density",side=4,col="red",line=4) 
    axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1)
    
    ## Draw the time axis
    axis(1,pretty(range(time),10))
    mtext("Time (Hours)",side=1,col="black",line=2.5)  
    
    ## Add Legend
    legend("topleft",legend=c("Beta Gal","Cell Density"),
      text.col=c("black","red"),pch=c(16,15),col=c("black","red"))
    

    enter image description here

    Similar recipes can be used to superimpose plots of different types – bar plots, histograms, etc..

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

Sidebar

Related Questions

Would like to be able to set colors of headings and such, different font
I would like to use a language that I am familiar with - Java,
I have a 320x480 PNG that I would like to texture map/manipulate on the
Would like to know options in 3D graphics engines out there that can even
would like to create a function that generates graphs using ggplot. For the sake
Would like to create a method that instantiate objects. - (NSArray *) make3Of :
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The

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.