0

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

bretauv
  • 7,756
  • 2
  • 20
  • 57
  • Hi, did you check [here](https://stackoverflow.com/questions/41338757/adding-percentage-labels-on-pie-chart-in-r) or [here](https://stackoverflow.com/questions/45657990/how-to-create-a-pie-chart-with-percentage-labels-using-ggplot2)? – bretauv Mar 30 '20 at 14:17
  • Hi, thanks for your answer. I already checked but I'm struggeling with the differences between my dataframe and the ones in the example. They have groups and values and can then use ```round(value / sum(value) * 100, 1``` to create the labels. – Matias Wagner Mar 31 '20 at 11:48
  • can you include the code to produce your data in your post please? Use `dput` on your data for this – bretauv Mar 31 '20 at 12:06
  • Hi, sure, it's ```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))``` Thanks, Matias – Matias Wagner Mar 31 '20 at 15:55

0 Answers0