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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:39:55+00:00 2026-06-08T15:39:55+00:00

Is it possible to have two legends with ggplot2 but based on different datasets

  • 0

Is it possible to have two legends with ggplot2 but based on different datasets ? For instance, in the code below I would like to get both the legend of the first situation and the legend of the second situation in the same graphic. My attempt (third situation) does not work.

library(ggplot2)
library(scales)

yrng <- range(economics$unemploy)
xrng <- range(economics$date)
presidential <- presidential[-(1:3), ]

# add a fictive factor to the economics dataset
economics <- cbind.data.frame(economics, col=gl(2, nrow(economics)/2))

#####################
## first situation ##
#####################
# first plot with legend
unemp <- qplot(date, unemploy, data=economics, geom="line",
                 xlab = "", ylab = "No. unemployed (1000s)", colour=col)
# second plot without legend
unemp + geom_vline(aes(xintercept = start), data = presidential)

######################
## second situation ##
######################
# first plot without legend
unemp <- qplot(date, unemploy, data=economics, geom="line",
                 xlab = "", ylab = "No. unemployed (1000s)")
# second plot with legend
unemp + 
  geom_rect(aes(NULL, NULL, xmin = start, xmax = end,
                    fill = party), ymin = yrng[1], ymax = yrng[2],
                    data = presidential) +
  scale_fill_manual(values = alpha(c("blue", "red"), 0.2))


#####################
## third situation ##
#####################
# first plot with legend
unemp <- qplot(date, unemploy, data=economics, geom="line",
                 xlab = "", ylab = "No. unemployed (1000s)", colour=col)
# second plot with legend
unemp + 
  geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party), ymin = yrng[1],
              ymax = yrng[2], data = presidential) + 
  scale_fill_manual(values = alpha(c("blue", "red"), 0.2))

Error in data.frame(xmin = 11342, xmax = 14264, fill = "Republican", colour = function   (x,  : 
  arguments imply differing number of rows: 1, 0
  • 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-08T15:39:56+00:00Added an answer on June 8, 2026 at 3:39 pm

    In general, once I start getting to more complicated plots, its almost always better to stop using qplot and use ggplot instead. I find it easier to think about how I’m building the plot up piece by piece:

    ggplot() +
      geom_line(aes(x=date, y=unemploy, color=col), data=economics) +
      geom_rect(aes(xmin=start, xmax=end, fill=party),
                ymin = yrng[1], ymax = yrng[2], data = presidential) +
      scale_fill_manual(values = alpha(c("blue", "red"), 0.2)) +           
      xlab("") +
      ylab("No. unemployed (1000s)")
    

    This gives:

    plot

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

Sidebar

Related Questions

Is it possible to have two methods with the same name but different parameters
Is it possible to have two different cache instances for Zend_Cache ? For instance,
I would like to know if it is possible to have two CADisplayLink in
Is it possible to have two background images? For instance, I'd like to have
Is this possible to have two (or more) different kinds of cells to be
Is it possible to have two different foci/focuses on X windows?
I have found possible slowdown in my app so I would have two questions:
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
I have two different versions of linux/unix each running cfengine3. Is it possible to
Is it possible to have two JDialogs popup at the same time, second below

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.