简体   繁体   中英

R : add number of observations on graph with fill and facet_wrap

Quite new with R and still struggling with graphs.

How to display the number of observations above all violin boxes in a facet_wrap graph that also differenciates a categorical variable by color?

Here is an example with the R built-in data set "CO2":

graph <- CO2 %>% ggplot(aes(x=Treatment, y=uptake, fill=Type)) + geom_violin(width=1) +   facet_wrap(vars(Plant))
graph + scale_y_continuous(limits=c(0, 50), position = "bottom", labels=scales::number) + labs(title= "CO2 dataset : Upate versus Treatment, Type and Plant", x="Treatment", y="Uptake") + scale_colour_discrete(guide = FALSE)

如何在小提琴盒顶部添加观察次数?

There are solutions for simpler graphs (with facets but without colored fill) but I did not manage to adapt them to this graph configuration.

Thanks !

Here is one way to do it, using a helper data frame with the totals and passing inherit.aes=FALSE to geom_text so it doesn't need the other aesthetics. I'm not sure if I am showing the number you want, that is easy to change by modifying totals .

totals = CO2 %>% 
  count(Plant) %>% 
  mutate(label=paste('Count:', n), Treatment='nonchilled', uptake=50)
CO2 %>% ggplot(aes(x=Treatment, y=uptake, fill=Type)) + 
  geom_violin(width=1) +   
  facet_wrap(vars(Plant)) +
  scale_y_continuous(limits=c(0, 60), position = "bottom", labels=scales::number) + 
  labs(title= "CO2 dataset : Upate versus Treatment, Type and Plant", x="Treatment", y="Uptake") + 
  scale_colour_discrete(guide = FALSE) +
  geom_text(data=totals, aes(Treatment, uptake, label=label), 
            vjust=0, hjust=0, inherit.aes=FALSE)

在此处输入图像描述

Based on @Kent-Johnson proposal, here is a code that works. 现在效果很好!

totals_nonchilled <- CO2 %>% 
  group_by(Plant, Treatment, .drop = FALSE) %>%
  count(Plant) %>% tidyr::spread(key=Treatment, value=n) %>%
  mutate(label=paste(nonchilled), Treatment='nonchilled', uptake=45)

totals_chilled <- CO2 %>% 
  group_by(Plant, Treatment, .drop = FALSE) %>%
  count(Plant) %>% tidyr::spread(key=Treatment, value=n) %>%
  mutate(label=paste(chilled), Treatment='chilled', uptake=45)

totals <- dplyr::bind_rows(totals_nonchilled, totals_chilled)

graph <- CO2 %>%
  ggplot(aes(x=Treatment, y=uptake, fill=Type)) +
  geom_violin(width=1) +
  facet_wrap(vars(Plant))

graph + scale_y_continuous(limits=c(0, 50), position = "bottom", labels=scales::number) + labs(title= "CO2 dataset : Upate versus Treatment, Type and Plant", x="Treatment", y="Uptake") + scale_colour_discrete(guide = FALSE) +
  geom_text(data=totals, aes(Treatment, uptake, label=label), 
            vjust=0, hjust=0, inherit.aes=FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM