Search code examples
rerror-handlingcompiler-errorspie-chart

R doesn't display percentages when trying to build a pie chart


This is the code I wrote :

gender=Survey$`To_which_gender_you_identify_the_most?`
gender<-as.factor(gender)
count=c("Prefer not to say","Male","Female")
pie(table(gender),labels=c(paste0(count)))

I'm trying to create a pie chart that displays percentages but the percentages don't show. Sample data

enter image description here

dput

> dput(head(Survey[1:4]))
structure(list(Horodateur = structure(c(1619171956.596, 1619172695.039, 
1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), `To_which_gender_you_identify_the_most?` = c("Male", 
"Female", "Male", "Female", "Female", "Female"), What_is_your_age_group = c("[18-24[", 
"[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["), How_much_time_do_you_spend_on_social_media = c("1-5 hours", 
"1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours"
)), row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"
))
> 

Solution

  • Updated. Based on the updated example data, this code will produce pie chart labeled with percentages as requested.

    #your example data
    Survey<-structure(list(
        Horodateur = structure(c(1619171956.596, 1619172695.039, 1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
        `To_which_gender_you_identify_the_most?` = c("Male", "Female", "Male", "Female", "Female", "Female"),
        What_is_your_age_group = c("[18-24[", "[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["), 
        How_much_time_do_you_spend_on_social_media = c("1-5 hours", "1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours")), 
    row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"))
    #make a dataframe of a gender table
    gender<-data.frame(table(Survey$`To_which_gender_you_identify_the_most?`))
    #add a percentages to the labels. Don't use 'count' to label unless you are sure it mataches the order of your data. More on that below. 
    pie(gender$Freq,labels=c(paste0(gender$Var1," ",round(100*gender$Freq/length(Survey$`To_which_gender_you_identify_the_most?`), digits = 0),"%")))
    

    Result:example of pie chart in R specific to this question

    Original. You would need to add some kind of percent calculation to your label= within pie(). All your current code is doing is labeling the pie chart with the character vector called count. Try this instead:

    #make a dataframe out of the table of your gender object  
    your_table<-data.frame(table(gender))
    #add a percentages to the labels. Don't use 'count' to label unless you are sure it mataches the order of your data. More on that below. 
    pie(your_table$Freq,labels=c(paste0(your_table$gender," ",round(100*your_table$Freq/length(gender), digits = 0),"%")))
    

    One thing to be careful of in your original code is that you are applying labels based on the vector called count, but the factor order in the gender object may or may not match. Let me show you how this could mislabel your pie().

    #This is an example of how you might mislabel your data.
    Survey<-data.frame(`To_which_gender_you_identify_the_most?`=c("Female","Male","Female","Male","Female", "Prefer not to say"))
    gender=Survey$`To_which_gender_you_identify_the_most`
    gender<-as.factor(gender)
    count=c("Prefer not to say","Male","Female")
    your_table<-data.frame(table(gender))
    pie(your_table$Freq,labels=c(paste0(count," ",round(100*your_table$Freq/length(gender), digits = 0),"%")))
    

    Note all the percentage labels are correct, but the text labels from count are in the wrong order in this example image.mislabeled pie chart created in R