0
votes

I'm creating a plot of rainfall means by month and year in R. The following plot is what I created using these columns: Year, Numbered_Months, Rainfall(mm/month)

My Plot

What I now want to do is add another line that shows the mean value for each month across years. Is there a way to do this with ggplot based on what I've already got plotted, rather than working out a new column into the dataframe and adding it as another 'year' category?

2
Providing your data as reproducible example and your code could help others to solve your problem, please check stackoverflow.com/questions/5963269/…knytt

2 Answers

1
votes

You could try this:

library(data.table)
library(ggplot2)

dt_a = data.table(year=c(2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019),
                month=c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12),
                value=runif(24, 10, 30))

ggplot(
  data=dt, aes(x=month, y=value)
) + geom_line(
  aes(color=factor(year))
) + stat_summary(
  fun = mean, geom="line"
)

enter image description here

1
votes

We can recreate your plot quite well with the following data and plotting code (it's always better to include this in your question)

df <- data.frame(Year = rep(2017:2019, each = 12),
                 Numbered_Months = rep(1:12, 3),
                 `Rainfall(mm/month)` = c(0.2, 0.25, 2.5, 4.9, 2.1, 1.2, 0.6, 
                                          0.3, 0.4, 0.6, 0.65, 0.75, 0.25, 0.15, 
                                          0.35, 0.55, 0.6, 0.8, 0.6, 0.55, 0.5, 
                                          3.4, 2.9, 2.1, 0.45, 0.4, 0.25, 0.8, 
                                          1.4, 0.15, 0.8, 0.85, 0.65, 1.4, 2.3, 
                                          0.3))

library(ggplot2)

original <- ggplot(df, aes(Numbered_Months, Rainfall.mm.month.)) +
  geom_line(aes(colour = factor(Year))) +
  scale_color_manual(values = c("red", "darkblue", "forestgreen")) +
  scale_y_continuous(breaks = 1:5) +
  scale_x_continuous(breaks = 1:12) +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.x = element_text(angle = 90),
        panel.grid.minor = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none")

original

We can either use the stat_summary function or just work out what we want to plot and pass that data to ggplot. In this example I have created a little summary data frame to add the monthly means along with the standard error:

library(dplyr)

group_means <- df %>% 
  group_by(Numbered_Months) %>% 
  summarize(mean = mean(Rainfall.mm.month.), 
            sem_low = mean- sd(Rainfall.mm.month.)/sqrt(3), 
            sem_high = mean + sd(Rainfall.mm.month.)/sqrt(3))
#> `summarise()` ungrouping output (override with `.groups` argument)

original +   
  geom_ribbon(data = group_means, 
              aes(y = mean, ymin = sem_low, ymax = sem_high),
              alpha = 0.1) +
  geom_line(data = group_means, aes(y = mean), linetype = 2, size = 1) 

Created on 2020-08-02 by the reprex package (v0.3.0)