https://whatalnk.github.io/r-tips/ggplot2-secondary-y-axis.nb.html
https://stackoverflow.com/questions/51456307/how-to-add-a-legend-for-the-secondary-axis-ggplot
Second link lets you add legend
d %>>% ggplot() +
geom_bar(mapping = aes(x = Date, y = Precipitation * 30 / 400), stat = "identity", colour = gray(0.5), fill = gray(0.5)) +
geom_line(mapping = aes(x = Date, y = Temperature)) +
geom_point(mapping = aes(x = Date, y = Temperature), size = 3, shape = 21, fill = "white") +
scale_x_date(name = "Month", breaks = seq.Date(as.Date("2015-01-01"), as.Date("2015-12-31"), by = "1 month"), labels = function(date){return(month(date, label = TRUE))}) +
scale_y_continuous(
name = expression("Temperature ("~degree~"C)"),
sec.axis = sec_axis(~ . * 400 / 30 , name = "Precipitation (mm)"),
limits = c(0, 30)) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
# my example
ggplot(data = total_cases, aes(x = quarter, y = count, group = 1) ) +
geom_bar(stat = "identity", fill = gray(0.5))+
geom_line(data = HIV_Q_data, size = 2, aes(x = quarter, y = Proportion*250/100, group = 1), colour = bc_col) +
xlab("Year-Quarter") + ylab("Infectious syphilis cases") +
theme_light()+
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))+
theme(plot.title = element_text(lineheight=.8, face="bold", size = 14)) +
theme(text = element_text(size=14))+
theme(panel.grid.minor.x = element_line(colour = "white")) +
theme(legend.position = "top", legend.title = element_blank()) +
scale_y_continuous(limits = c(0,250), expand = c(0, 0),
sec.axis = sec_axis(~. *100/250, name = "% HIV positive cases")) +
scale_x_discrete(expand = c(0, 0))