geom_vline in facet_grid

Dear R users,

I want to draw mean line of different seasons for each legends. I tried but I could only draw the aggregate mean line for each legends (not seasonally)
my code is

DATA585 %>% 
  mutate(season1 = factor(season, levels = c("DJF", "MAM", "JJAS","ON"))) -> DATA585a
ggplot(DATA585a, aes(ST, ..scaled.. , color=region, fill = region))+
  geom_density(alpha=0.2, size=0.5) +
  theme_test() + 
  theme(legend.title=element_blank()) +
  theme(axis.title = element_text(face="plain",size=14,color="black"),
        axis.text=element_text(size=10,face="plain", color="black"),
        axis.title.x = element_text(vjust=0.1),
        axis.text.y=element_text(hjust=0.5),
        plot.title = element_text(size=15)) +
  ylab("density") +
  xlab("ST (K)") +
  #xlab(expression(paste("SA (", kg/m^2/d, ")", sep = ""))) +
  labs(title= "") +
  scale_y_continuous(breaks = c(0, 0.5, 1), limits = c(0,1), expand = c(0.1,0.1))+
  scale_x_continuous(breaks = c(-1,0,1,2,4,6,8,10), limits = c(-1.07,10), expand = c(0.1,0.1))+
  facet_grid(season1 ~ .) +
  theme(strip.text.y = element_text(size=13)) ->p

mu <- ddply(DATA585a, "region", summarise, grp.mean=mean(ST))
p + geom_vline(data=mu, aes(xintercept=grp.mean, color=region),
               linetype="dashed")

Rplot02

data can be found Dropbox - File Deleted - Simplify your life
So how could I draw mean line for each legends (area) of different season?

In ddply, group by season1 and region to get within-group means.

Also, plyr is an old, retired package. I suggest switching to dplyr (and other tidyverse packages). Then the code would be:

library(tidyverse)

mu <- DATA585a %>%
       group_by(region, season1) %>%
       summarise(grp.mean=mean(ST))
1 Like

Thanks, It worked :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.