Search code examples
rdatasetpie-chartdata-analysispercentage

Display Pie Chart Percentages in R studio from Dataset


I'm trying to display the percentages of distinct value occurrences for each column in my dataset using R.

This simple line of code creates a table from the specified column and displays a perfect pie chart with the correct distributions. However, I can't seem to display the percentage values.

I believe there is a short and simple way to do this.

This is what i have so far. Please what do i need to add?

> workclass <- table(adult$workclass)
> pie(workclass) 

Thanks.

These are the values in my "workclass" column that i have a pie chart for. I just need to display their percentage distributions on the pie chart.

Federal-gov - 1836 Occurrences     
Local-gov - 960 Occurrences
Never-worked - 2093 Occurrences
Private - 120 Occurrences
Self-emp-inc - 2541 Occurrences
Self-emp-not-inc - 1116 Occurrences
State-gov - 2093 Occurrences
Without-pay - 1298 Occurrences

Solution

  • I hope this code works for you, to display the percentages in the pie chart.

    adult <- data.frame(workclass = c(rep("Federal-gov",1836),rep("Local-gov",960),rep("Never-worked",2093),
                                      rep("Private",120), rep("Self-emp-inc",2541), rep("Self-emp-not-inc",1116),
                                      rep("State-gov",2093),rep("Without-pay",1298))) 
               Occurrences = c(1836,960,2093,120,2541,1116,2093,1298))
    
    workclass <- table(adult$workclass)
    
    
    par(mar = c(2,2,2,2))
    lb = paste0(round(prop.table(workclass)*100,2),"%")
    pie(workclass,labels = lb, col = rainbow(8))
    legend(-2.1,0.4,legend=names(workclass),cex=0.7,yjust=0.2, xjust = -0.1,
           fill = rainbow(8), bty = "n")
    

    enter image description here

    prop.table(workclass)
    Federal-gov        Local-gov     Never-worked          Private     Self-emp-inc Self-emp-not-inc 
         0.152276686      0.079621796      0.173592104      0.009952725      0.210748943      0.092560338 
           State-gov      Without-pay 
         0.173592104      0.107655304