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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:01:57+00:00 2026-06-11T02:01:57+00:00

I would like put a bar and a line plot of two separate but

  • 0

I would like put a bar and a line plot of two separate but related series on the same chart with a legend (the bar plot is of quarterly growth the line plot is of annual growth).

I currently do it with a data.frame in wide format and code like this:

p <- ggplot() +
    geom_bar(df, aes(x=Date, y=quarterly), colour='blue') +
    geom_line(df, aes(x=Date, y=annual), colour='red')

but I cannot work out how to add a legend, which has a red line labeled ‘Annual Growth’; and a blue square labeled ‘Quarterly Growth’.

Alternatively, I cannot work out how to have differnt geoms for different series with a long-form data.frame.

UPDATE:

The following example code gets me part of the way towards a solution, but with a really ugly duplicate legend. Still looking for a complete solution … This approach is based on putting the data in long form and then plotting subsets of the data …

library(ggplot2)
library(reshape)
library(plyr)
library(scales)

### --- make a fake data set
x <- rep(as.Date('2012-01-01'), 24) + (1:24)*30
ybar <- 1:24
yline <- ybar + 1

df <- data.frame(x=x, ybar=ybar, yline=yline)
molten <- melt(df, id.vars='x', measure.vars=c('ybar', 'yline'))
molten$line <- ifelse(molten$variable=='yline', TRUE, FALSE)
molten$bar <- ifelse(molten$variable=='ybar', TRUE, FALSE)

### --- subset the data set
df.line  <- subset(molten, line==TRUE)
df.bar   <- subset(molten, bar==TRUE)

### --- plot it
p <- ggplot() +
geom_bar(data=df.bar, mapping=aes(x=x, y=value, fill=variable, colour=variable),
    stat='identity', position='dodge') +
geom_line(data=df.line, mapping=aes(x=x, y=value, colour=variable)) +

opts(title="Test Plot", legend.position="right") 

ggsave(p, width=5, height=3, filename='plot.png', dpi=150)

And an example plot …

enter image description here

  • 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-11T02:01:58+00:00Added an answer on June 11, 2026 at 2:01 am

    By use of the subset argument to geoms.

    > x=1:10;df=data.frame(x=x,y=x+1,z=x+2)
    > ggplot(melt(df),
        aes(x,value,color=variable,fill=variable))+
      geom_bar(subset=.(variable=="y"),stat="identity")+
      geom_line(subset=.(variable=="z"))
    

    enter image description here

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

Sidebar

Related Questions

I would like to put a progressBar on the action bar but setting requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
I would like to put jQuery at the bottom of the body tag but
I would like to put a large variable definition in a separate file for
I would like to put a progress bar in the notification bar. The idea
I would like to put a gradient on the bottom layout, but it seems
I would like to put newlines in a property value of a manifest. Its
I would like to put a button on top of a Google Map next
I would like to put a placeholder into e.g. the footer.html and have it
I would like to put a time & date stamp on each row added
I would like to put my local OS X wordpress site in my ~users/Sites/public

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.