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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:38:14+00:00 2026-06-18T01:38:14+00:00

How can I plot lines refering to Y1 , and bars refering to Y2

  • 0

How can I plot lines refering to Y1, and bars refering to Y2 in the same plot in ggplot2?

This is my data:

   year       partbuild    mean.t       sd.t  n.t      se.tr       ci1       ci2
1  2003          Assets  6.072719  11.109798  173  0.8446623  4.417181  7.728257
2  2003 Non-opportunity  3.793043  59.377032 4394  0.8957534  2.037366  5.548720
3  2003     Opportunity  2.650684   7.397618  257  0.4614507  1.746240  3.555127
4  2004          Assets 11.334394  19.609274  173  1.4908655  8.412297 14.256490
5  2004 Non-opportunity  5.922468  38.776455 4394  0.5849760  4.775915  7.069021
6  2004     Opportunity  4.757593  21.598943  257  1.3473051  2.116875  7.398311
7  2005          Assets 13.580937  23.748005  368  1.2379504 11.154554 16.007319
8  2005 Non-opportunity  9.698966 154.769250 4009  2.4443683  4.908004 14.489928

This is the code I’m using

y.bar <- ddply(d1a, c('year', 'partbuild'), function(x) mean(x$transfers.cap, na.rm=T))
sd.tr <- ddply(d1a, c('year', 'partbuild'), function(x) sd(x$transfers.cap, na.rm=T)) 
n.tr <- ddply(d1a, c('year', 'partbuild'), function(x) length(x$transfers.cap))
db2 <- merge(y.bar, sd.tr, by=c('year', 'partbuild'))
db3 <- merge(db2, n.tr, by=c('year', 'partbuild'))
colnames(db3) <- c('year', 'partbuild', 'mean.t', 'sd.t', 'n.t')

p <- ggplot(db3, aes(y=mean.t, x=year, group=partbuild, shape=partbuild))
p + geom_point(aes(colour = partbuild), size=3.5) + geom_line(aes(colour = factor(partbuild)), size=1) + theme_bw() + ylab("Average of Federal Transfers per capita") + xlab("Year") + theme(axis.title.x = element_text(face='bold', size = 15, vjust = .25)) + theme(legend.title=element_blank()) + theme(axis.title.y = element_text(face="bold", size = 15, angle=90)) + scale_fill_discrete(name="") + theme(legend.text = element_text(colour="black", size = 13, face = "bold"))  + geom_vline(xintercept = c(2004, 2008), linetype="dashed" )

And this is the picture I get:

Sample plot

However, I’d like to plot areas with the variable n.tr behind these lines.

How can I do this?

  • 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-18T01:38:15+00:00Added an answer on June 18, 2026 at 1:38 am

    Plot shows only part of your data because only the first 8 lines were provided.

    First, in function ggplot() values for x= and colour= are provided as those values are the same for all layers. Then for each element y= values are provided. Used scale_y_log10() because there is large difference between n.t and mean.t values.

    p<-ggplot(data=db3,aes(x=year,colour=partbuild))
    p+geom_bar(aes(y=n.t,fill=partbuild),stat="identity",position="dodge")+
     geom_line(aes(y=mean.t))+
      geom_point(aes(y=mean.t,shape=partbuild), size=3.5)+scale_y_log10()+theme_bw()
    

    enter image description here

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

Sidebar

Related Questions

How can I use plot to turn this plot sideways so the histogram bars
I created this plot with ggplot2 : The outside lines need to correspond to
I want to have multiple lines on the same plot. Multiple data points. In
How can I move a ggplot2 legend to the bottom of the plot and
I need to get a plot that fits the data automatically using matplotlib. This
Normally when using interactively gnuplot I do: gnuplot> plot state_log_6548032.data using 4 with lines
How can I have a line graph with categories, but plot the lines in
Can you draw the grid lines in a plot with parasitic axes in matplotlib?
I like to plot two groups of lines in the same plot. Each group
I can capture data from serial device via pyserial, at this time I can

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.