I'm trying to replicate the graph found here: Bar&LineChartCombo. What I would like to do is create a bar chart which will include the total Amount
value at the top of the bar chart. I would also like to include a total Rate
line plot over the bar chart, ideally starting in the middle of the bar chart. My problem is, I can get the total Amount
above the bar chart but I cannot figure out the correct way to overlay just the total Rate
and display the total Rate
value. If this is possible, I would also like to exclude a secondary axis but from what I've read it is needed to properly scale the secondary graph being overlaid.
Below is a random graph I found which demonstrates the point and line overlay I'm looking to create:
df<-data.frame(
Group=c("A","B","C","D","E","Total","A","B","C","D","E","Total","A","B","C","D","E","Total"),
Date=c("Feb 2021","Feb 2021","Feb 2021","Feb 2021","Feb 2021","Feb 2021","Apr 2021","Apr 2021","Apr 2021","Apr 2021","Apr 2021","Apr 2021","Jun 2021","Jun 2021","Jun 2021","Jun 2021","Jun 2021","Jun 2021"),
Amount=c(100,200,300,400,500,1500,200,400,600,800,1000,3000,400,800,1200,1600,2000,6000),
Rate=c(3.5,3.45,3.39,3.44,3.51,3.48,3.53,3.42,3.41,3.47,3.52,3.50,3.51,3.40,3.44,3.45,3.50,3.49))
ggplot(df,aes(x=factor(Date), y=Amount,fill=Group)) +
geom_bar(stat="identity", colour="sienna3")+
geom_line(aes(x=factor(Date), y=(Rate)*max(Amount)),stat="identity")+
geom_text(aes(label=Rate, x=factor(Date), y=Rate*max(Amount)), colour="black")+
geom_text(aes(label = format(round(stat(y),0),big.mark=","), group = factor(Date)),stat = 'summary', fun = sum, vjust = -.25)+
scale_y_continuous(sec.axis = sec_axis(~./max(df$Amount)))