Overlaying a bar chart with a point and line w/o a secondary axis

Hello,

I'm trying to create a bar chart with a point and line overlay. I'm trying to recreate the graph in the picture attached:

image

Ideally, I'd like to exclude the secondary axis since the purpose of overlaying the line is to show the sensitivity of the rate with changes in the amount. From the reading I've done, I don't think ggplot2 will allow an overlay without the secondary axis. My code below overlays my bar chart with points but both y axis are scaled incorrectly. Does anyone have a suggestion on how to correctly rescale the y-axis or better yet how to overlay the bar chart without the secondary axis?

df<-data.frame(
    Group=c("A","B","C","D","E","A","B","C","D","E","A","B","C","D","E"),
    Date=c("Feb 2021","Feb 2021","Feb 2021","Feb 2021","Feb 2021","Apr 2021","Apr 2021","Apr 2021","Apr 2021","Apr 2021","Jun 2021","Jun 2021","Jun 2021","Jun 2021","Jun 2021"),
    Amount=c(1000,1300,1500,1600,1100,1200,1700,1500,1900,1300,1200,1300,1700,1800,1500))

df2<-data.frame(Date=c("Feb 2021","Apr 2021","Jun 2021"),Rate=c(3.51,3.49,3.53),Amount=c(6500,7600,7500))

ggplot(df,aes(x=Date, y=Amount, group=Group, fill = Group)) + 
    geom_bar(stat="identity", colour="sienna3")+
    geom_point(data=df2,aes(x=Date,y=Rate),inherit.aes = FALSE)+
    geom_text(aes(label = format(round(stat(y),0),big.mark=","), group = factor(Date)),stat = 'summary', fun = sum, vjust = -.25)+
    geom_text(data=df2,aes(label=Rate, x=Date, y=Rate*(Amount)), colour="black")+
    scale_y_continuous(sec.axis = sec_axis(~./max(df$Amount)))
    

In your geom_point, the y is set to Rate/(Rate/Amount) which is the same as Amount. What are you trying to plot as the y values of geom_point?

Sorry about that, I'm trying to plot the Rates from the df2 dataset as the y values of the geom_point.

Here is a start to getting the Rates on the graph. Is that anything like what you want?

library(ggplot2)
df<-data.frame(
  Group=c("A","B","C","D","E","A","B","C","D","E","A","B","C","D","E"),
  Date=c("Feb 2021","Feb 2021","Feb 2021","Feb 2021","Feb 2021","Apr 2021","Apr 2021","Apr 2021","Apr 2021","Apr 2021","Jun 2021","Jun 2021","Jun 2021","Jun 2021","Jun 2021"),
  Amount=c(1000,1300,1500,1600,1100,1200,1700,1500,1900,1300,1200,1300,1700,1800,1500))

df2<-data.frame(Date=c("Feb 2021","Apr 2021","Jun 2021"),Rate=c(3.51,3.49,3.53),Amount=c(6500,7600,7500))

ggplot() + 
  geom_bar(data=df,mapping=aes(x=Date, y=Amount, group=Group, fill = Group),
           stat="identity", colour="sienna3")+
  geom_point(data=df2,aes(x=Date,y=Rate*1000),inherit.aes = FALSE)+
  geom_text(aes(x=Date,y=Rate*1000,label = Rate),data=df2, vjust = -.25)

Created on 2021-11-30 by the reprex package (v2.0.1)

1 Like

Thanks so much for the help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.