I'm quite new with R and I'm trying to create facetted pie charts with percentages. My data is a dataframe with 31 patients. These either have a rapid progression of the disease or a slow progression. And each patient has a genetic diagnosis.
Here's the data
pie <- structure(list(progression = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("rapid", "slow"), class = "factor"), Gene = structure(c(3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 5L, 5L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 2L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L), .Label = c("ALS2", "BSCL2", "C9orf72", "C9orf72/SOD1", "FUS", "FUS/TBK1", "SETX", "SOD1 het", "SOD1 hom"), class = "factor")), class = "data.frame", row.names = c(NA, -31L))
I have now created a facetted pie chart using the following R code:
library(ggplot2)
ggplot(data = pie, aes(x=factor(1), fill=Gene)) +
geom_bar(position="fill", aes(y = stat(count/sum(count)))) +
facet_grid(facets=. ~ progression) +
coord_polar("y") +
scale_y_continuous(labels = scales::percent_format())
I tried to add the percentage of each genetic diagnosis among the rapid and the slow progression patients using geom_text but I have always failed.
It would be great to have your help here!
Many thanks, Matias