I am trying to make a plot using ggplot, which shows the rainfall from 3 months, over 4 years (12 months data in total). I have used individual points to show the individual months, and have added a line plot to join the same months between the four different years.
I have then used a bar plot to calculate and show the total for each quarter. My code is below, and the plot itself and the dataframe I used to build it is below:
# graph rainfall, showing individual months as dots and totals for each quarter as bars
ggplot(df_q4_month, aes(x = as.factor(Year), y = total_rainfall)) +
geom_point(size = 1.5, aes(colour = as.factor(Month))) +
geom_line(aes(group = Month, colour = as.factor(Month))) +
scale_colour_discrete("Month", labels = c("10" = "October",
"11" = "November",
"12" = "December")) +
#stat_summary(aes(y = total_rainfall), fun = "sum", geom = "bar", alpha = 0.25, width = 0.5) +
geom_bar(stat = "summary", fun = "sum", alpha = 0.15, width = 0.5, fill = "brown") +
labs(x = "Year", y = "Rainfall, mm", fill = "Total for quarter") +
ggtitle("Rainfall in Quarter 4 by month, and total for Quarter 4 per year") +
theme_bw()
My question is how can I get a second legend which refers to what the bars mean (ie. total rainfall for the quarter). However, this statistic is not a value in a cell in the dataframe, nor a variable which I can refer to. Ideally I would like for the second, separate legend to have a title (like "Month" in the existing legend), with an object of colour = "brown" labelled as showing the "Total rainfall in quarter".
I have tried using scale_fill_manual but I can't get it to work, as each quarterly total is a different value of the same variable, but the code has no variable to refer to in the dataframe (as it was generated by summary statistics). Is there any way to keep using the approach with geom_bar(stat = "summary") and add this legend, without creating another dataframe?
Other ideas which don't use this exact approach are also welcome. I also have tried making the geom_bar component with the below code layer, but I hit the same problems:
stat_summary(aes(y = total_rainfall), fun = "sum", geom = "bar")
Thank you very much


