Home > database >  Creating Dual Axis plot (bar and line)
Creating Dual Axis plot (bar and line)

Time:01-18

I want to create a dual axis plot in ggplot R with a dual bar and line plot, like this one created in excel.

enter image description here

The y axis scales are different.

my data is as follows;

enter image description here

I've created a bar plot and line plot. But unsure on how to put them together (I've tried man various ways and they dont seem to work).

Here is my code for the bar plot.

inf_conc <- ggplot(data=data, aes(x=Day, y=inf))  
 geom_bar(stat="identity", width=0.4, color="red3", fill="red3") 

 ggtitle("Influent Microplastic Concentration \n and Flow Rate")  # \n splits long titles into multiple   lines
  xlab("Day")   
  ylab("Microplastic Concentration (MPs/L)") 
  scale_y_continuous(limits =c(0, 50), breaks = seq(0, 50, 5))

inf_conc   theme(axis.text = element_text(size = 20,  colour = "black"), plot.title = element_text   (size =25,  hjust = 0.5, face = "bold"), axis.title = element_text(size = 20,  face = "bold", margin = 5))
inf_conc   theme(axis.text = element_text(size = 20,  colour = "black"), plot.title = element_text (size =25,  hjust = 0.5, face = "bold"), axis.title = element_text(size = 20, face = "bold", margin = 20))

and here is the code for the line plot:

inf_flow <- ggplot(data=data, aes(x=Day, y=flow, group = 1)) 
  geom_line(stat = "identity", colour ="blue4") 
  geom_point(colour ="blue4") 
   ylab("Inlet flow L/s") 
  xlab("Day") 
scale_y_continuous(limits=c(0,800), breaks = seq(0, 800, 100))


inf_flow   theme(axis.text = element_text(size = 20,  colour = "black"), plot.title = element_text (size =25,  hjust = 0.5, face = "bold"), axis.title = element_text(size = 20,  face = "bold", margin = 5))
inf_flow   theme(axis.text = element_text(size = 20,   colour = "black"), plot.title = element_text (size =25,  hjust = 0.5, face = "bold"), axis.title = element_text(size = 20, face = "bold", margin = 20))

Can anyone help with how I can get these onto one dual axis graph please.

CodePudding user response:

GGplot doesn't make it especially easy, but you can do it:

library(ggplot2)
my_dat <- data.frame(
  Day = paste("Day",rep(1:3, each=3), rep(c("(AM)", "(Midday)", "(PM)"), 3), sep= " "), 
  day_num = 1:9, 
  inf = seq(from = 13,to = 45, length=9), 
  flow = runif(9, 580, 740)
)

ggplot()   
  geom_bar(data=my_dat, aes(x=day_num, y=inf, fill = "Influent Concentration"), stat="identity", width=.6)   
  geom_line(data=my_dat, aes(x=day_num, y=flow*(50/800), colour="FLow Rate. L/s"))   
  scale_fill_manual(values="red")   
  scale_colour_manual(values="blue")   
  scale_x_continuous(breaks=1:9, labels=my_dat$Day)   
  scale_y_continuous(sec.axis = sec_axis(trans = ~.x*800/50, name = "Flow Rate L/S"), limits = c(0,50), name = "Influent. MPs/L")   
  labs(fill="", colour="", x="")   
  theme(legend.position="bottom", 
        axis.text.x = element_text(angle=45, hjust=1))

Created on 2023-01-17 by the reprex package (v2.0.1)

The main things you have to do are to

  1. Transform the second-axis series to have the same range(ish) as the first-series axis. In your case, the excel graph had the second y-axis going from 0-800 and the first y-axis going from 0-50, so the transformation is simple, you multiply the second series values by 50/800.

  2. In the scale_y_continuou() function there is an argument sec.axis which allows you to plot a second axis on the right-hand side of the plot. Here, you need to specify the trans argument to transform the values you're plotting back into the original values. That's what trans = ~.x*800/50 does.

  •  Tags:  
  • r
  • Related