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

  • Home
  • SEARCH
  • 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 8538519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:08:14+00:00 2026-06-11T11:08:14+00:00

Context I want to plot two ggplot2 on the same page with the same

  • 0

Context

I want to plot two ggplot2 on the same page with the same legend. http://code.google.com/p/gridextra/wiki/arrangeGrob discribes, how to do this. This already looks good. But… In my example I have two plots with the same x-axis and different y-axis. When the range of the the y-axis is at least 10 times higher than of the other plot (e.g. 10000 instead of 1000), ggplot2 (or grid?) does not align the plots correct (see Output below).

Question

How do I also align the left side of the plot, using two different y-axis?

Example Code

x = c(1, 2)
y = c(10, 1000)
data1 = data.frame(x,y)
p1 <- ggplot(data1) + aes(x=x, y=y, colour=x) + geom_line()

y = c(10, 10000)
data2 = data.frame(x,y)
p2 <- ggplot(data2) + aes(x=x, y=y, colour=x) + geom_line()


# Source: http://code.google.com/p/gridextra/wiki/arrangeGrob
leg <- ggplotGrob(p1 + opts(keep="legend_box"))
legend=gTree(children=gList(leg), cl="legendGrob")
widthDetails.legendGrob <- function(x) unit(3, "cm")
grid.arrange(
  p1 + opts(legend.position="none"),
  p2 + opts(legend.position="none"),
  legend=legend, main ="", left = "")

Output

Example image

  • 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-06-11T11:08:15+00:00Added an answer on June 11, 2026 at 11:08 am

    If you don’t mind a shameless kludge, just add an extra character to the longest label in p1, like this:

    p1 <- ggplot(data1) +
        aes(x=x, y=y, colour=x) +
        geom_line() + 
        scale_y_continuous(breaks = seq(200, 1000, 200),
                           labels = c(seq(200, 800, 200), " 1000"))
    

    I have two underlying questions, which I hope you’ll forgive if you have your reasons:

    1) Why not use the same y axis on both? I feel like that’s a more straight-forward approach, and easily achieved in your above example by adding scale_y_continuous(limits = c(0, 10000)) to p1.

    2) Is the functionality provided by facet_wrap not adequate here? It’s hard to know what your data structure is actually like, but here’s a toy example of how I’d do this:

    library(ggplot2)
    
    # Maybe your dataset is like this
    x <- data.frame(x = c(1, 2),
                    y1 = c(0, 1000),
                    y2 = c(0, 10000))
    
    # Molten data makes a lot of things easier in ggplot
    x.melt <- melt(x, id.var = "x", measure.var = c("y1", "y2"))
    
    # Plot it - one page, two facets, identical axes (though you could change them),
    # one legend
    ggplot(x.melt, aes(x = x, y = value, color = x)) +
        geom_line() +
        facet_wrap( ~ variable, nrow = 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context and Question: I want to add a legend to a lattice plot in
Context I want to use JavaFx with clojure. I am aware of http://nailthatbug.net/2011/06/clojure-javafx-2-0-simple-app/ Question:
I want to get the context of selected text in web page which means
I want to plot data against two Y-axis vs. X-axis. I debug and did
I have a list of schools that I want to plot on a Google
I have a bitmap image context and want to let this appear blurry. So
My question is exactly that but in context I want to examine the selection
Context If I want Lein 1.7 to start out with a particular class loaded,
I want to use Context.Items to store some info of a User Control like:
I want to remove a specific context menu item, appears when the Mouse down(right)

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.