I have looked at this articles: Legend formatting for ggplot with geom_col and geom_line and: Controlling legend appearance in ggplot2 with override.aes
I have a plot with a geom_col layer and 4 geom_line layers. I would like to be able to show a legend with no title using the colors that I chose when generating each layer in Plot 1. Alternatively, if I could change the colors and legend title in Plot 2 that might work. I would also like to change the Y-axis scale since the production data will never drop below 20 acre-feet but when I use scale_y_continuous with geom_col the bars do not appear.
Data
structure(list(Month_Day = structure(c(18628, 18629, 18630, 18631,
18632, 18633, 18634, 18635, 18636, 18637), class = "Date"), `2021` = c(37.57464179,
35.95859072, 39.14746726, 37.9630674, 40.55096688, 42.53487456,
41.66243889, 40.18150773, 37.90217088, 38.30979013), Mean = c(36.9619627272733,
36.2260000000002, 36.4146654545455, 36.3741972727362, 36.9979654545361,
36.5846318181822, 35.7040999999992, 36.3543818181826, 36.3387527272723,
36.5865854545457), Median = c(36.44704, 35.9190000000022, 35.56288,
36.19223, 35.82997, 36.21986, 35.69489, 35.07475, 36.29047, 36.18302
), Maximum = c(40.2170000000067, 42.43354, 41.04897, 39.2959999999978,
42.3659999999978, 40.524, 41.24238, 41.48184, 38.49166, 39.26223
), Minimum = c(34.1998, 31.78064, 33.38932, 34.74012, 33.96955,
31.54425, 31.48285, 33.49063, 35.02563, 34.7831)), row.names = c(NA,
10L), class = "data.frame")
Plot 1
library(ggplot2)
library(scales)
p_2021 <- ggplot(dfinal21, aes(x = Month_Day)) +
geom_col(aes(y = `2021`), size = 1, color = "darkblue", fill = "white") +
geom_line(aes(y = `Median`), size = 1.0, color="red") +
geom_line(aes(y = `Mean`), size = 1.0, color="yellow") +
geom_line(aes(y = `Maximum`), size = 1.0, color="darkgreen") +
geom_line(aes(y = `Minimum`), size = 1.0, color = "darkviolet") +
scale_x_date(breaks = "1 month", minor_breaks = "1 day", labels=date_format("%b")) +
labs(x = "Month", y = "acre-feet", title = "Treatment Plant Production 2021")
Generates this plot:
Plot 2
library(ggplot2)
library(scales)
p_2021 <- ggplot(dfinal21) +
geom_col(aes(x = Month_Day, y = `2021`, color = "2021")) +
geom_line(aes(x = Month_Day, y = `Median`, color = "Median")) +
geom_line(aes(x = Month_Day, y = `Mean`, color= "Mean")) +
geom_line(aes(x = Month_Day, y = `Maximum`, color= "Maximum")) +
geom_line(aes(x = Month_Day, y = `Minimum`, color= "Minimum")) +
scale_x_date(breaks = "1 month", minor_breaks = "1 day", labels=date_format("%b")) +
labs(x = "Month", y = "acre-feet", title = "Treatment Plant Production 2021") +
theme(legend.background = element_rect(fill = "white"))
Generates this plot: