2

I've drawn this quartile for the runoff rate, and I'd like the legend to appear as: - Median and - Q25-Q75. Can you please help me?

Runoff <- data.frame(Year = rep(1991:2002, each = 12),
                        Value=c(0.279,0.297,0.286,0.273,0.265,0.239,0.195,0.171,0.18,0.224,0.259,0.256,0.269,0.281,0.282,0.284,0.265,0.222,0.182,0.166,0.18,0.206,0.234,0.245,0.268,0.279,0.279,0.263,0.262,0.231,0.186,0.163,0.167,0.2,0.232,0.252,0.268,0.272,0.273,0.269,0.255,0.224,0.181,0.162,0.174,0.206,0.212,0.23,0.253,0.269,0.26,0.255,0.249,0.212,0.178,0.159,0.162,0.204,0.234,0.247,0.263,0.276,0.279,0.275,0.244,0.207,0.172,0.163,0.16,0.174,0.223,0.222,0.265,0.272,0.264,0.253,0.237,0.198,0.163,0.145,0.147,0.2,0.228,0.245,0.259,0.275,0.262,0.254,0.26,0.219,0.175,0.152,0.166,0.195,0.226,0.217,0.258,0.277,0.26,0.261,0.236,0.21,0.182,0.163,0.163,0.205,0.236,0.223,0.268,0.286,0.279,0.269,0.254,0.236,0.189,0.153,0.165,0.219,0.248,0.238,0.232,0.269,0.274,0.255,0.253,0.221,0.177,0.153,0.161,0.219,0.236,0.241,0.258,0.279,0.265,0.269,0.261,0.222,0.18,0.162,0.174,0.196,0.215,0.239
))

Runoff_stats <- Runoff |> group_by(Year) |> 
  summarize(Median = median(Value), Q25 = quantile(Value, probs = 0.25), Q75 = quantile(Value, probs = 0.75))
Runoff_stats

ggplot(Runoff_stats, aes(x = Year)) + geom_line(aes(y = Median), linetype=1) + 
  geom_ribbon(aes(ymin = Q25, ymax = Q75), fill = "green", alpha = 0.25) +
  scale_fill_manual(values = c("Median"='black', "Q25-Q75"='green'))+
  scale_x_continuous(breaks = 1991:2002)+ 
  labs(y= "Runoff (m3/s)", x= "Year")+ 
  theme(axis.title.x = element_text(face= "bold.italic", size=8,color= "#000000", hjust=0.5),
        axis.title.y = element_text(face= "bold.italic", color = "#000000", size=8),
        axis.line = element_line(linewidth = 0.5, color = "#000000"),
        plot.title = element_text(vjust = 10, hjust= 0.01, size = 10),
        legend. Position = "topright")

https://i.sstatic.net/TzDZFDJj.png

1 Answer 1

4

When you want a legend you have to map on aesthetics, i.e. map on the color and fill aes inside aes(). To apply the colors specified in scale_fill_manual to both color and fill use aesthetics = c("fill", "color") and to merge both legends set name = NULL or whatever title you want for your legend. Finally, "topright" is no valid legend.position. Instead you could use legend.justification="top" with legend.position="right":

library(ggplot2)

ggplot(Runoff_stats, aes(x = Year)) +
  geom_line(aes(y = Median, color = "Median"), linetype = 1) +
  geom_ribbon(aes(ymin = Q25, ymax = Q75, fill = "Q25-Q75"), alpha = 0.25) +
  scale_fill_manual(
    values = c("Median" = "black", "Q25-Q75" = "green"),
    aesthetics = c("fill", "color"),
    name = NULL
  ) +
  scale_x_continuous(breaks = 1991:2002) +
  labs(y = "Runoff (m3/s)", x = "Year") +
  theme(
    axis.title.x = element_text(face = "bold.italic", size = 8, color = "#000000", hjust = 0.5),
    axis.title.y = element_text(face = "bold.italic", color = "#000000", size = 8),
    axis.line = element_line(linewidth = 0.5, color = "#000000"),
    plot.title = element_text(vjust = 10, hjust = 0.01, size = 10),
    legend.justification = "top",
    legend.position = "right"
  )

1
  • Thank you so much Mr stefan, it was useful.
    – Barre
    Commented May 20 at 12:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.