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.