Search code examples
rsummultiple-columnspie-chartpercentage

Create a pie chart of multiple columns


I want to create a pie chart of multiple columns. This somewhat unusual constellation is necessary for the evaluation of a large survey. The answers were always yes/no, with several columns overlapping with yes/no, so simply merging them would change the statement. In the end, an absolute number of yes answers would be needed per associated column in total.

#example table:

x <- read.csv2(file = textConnection("a;b;c;d
yes;no;yes;no
no;no;yes;no
no;no;yes;no
no;yes;no;no
no;yes;no;no
no;no;no;yes
no;no;no;yes
no;no;yes;yes
no;no;yes;yes
no;yes;yes;no
yes;yes;no;no
yes;yes;no;no
yes;yes;no;no
yes;yes;yes;no
yes;no;yes;no
yes;no;yes;yes
yes;no;yes;yes
no;no;no;yes
no;no;no;no
no;no;no;no
no;no;yes;no
no;no;yes;no
yes;no;no;no
yes;no;no;no"))

In the end, there should be a pie chart showing in percent the a's amount of yes (x%), b's amount of yes (y%)... and so on.

Thanks in advance for your help.


Solution

  • Reshape from wide-to-long, keep only "yes" rows, then get counts and plot:

    d <- stack(x)
    tbl <- table(d[ d$values == "yes", "ind" ])
    pie(tbl, labels = paste(names(tbl), "-",
                            round(prop.table(tbl) * 100), "%"))
    

    enter image description here