How to create a pie chart from a dataset

Hi everybody, I have a question for you:
I need to make a pie chart with the % of the count of ID per each parameters (see the link of the Reprex: https://www.dropbox.com/s/ma5wqmegy6ud8q1/dataset%20example.csv?dl=0
I need to show in a pie chart data for the parameter Pr as follow:
for example, for the Pr = 1, I have 3000 id and I need to represent the % in a pie chart, respect to other values.
For the Pr = 2, I have 4500 id etc......

I have used pie() and also ggplot2 but they don't work...How can I do?
Thanks

That is not a reprex, that is a link to an unnecessarily large .csv file (please read the guide), here is a basic example of how to do a pie chart with ggplot2 and one of your variables.

If you need further help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

library(tidyverse)

sample_data <- read_delim("https://www.dropbox.com/s/ma5wqmegy6ud8q1/dataset%20example.csv?dl=1", delim = ";")

sample_data %>%
    count(Pr) %>% 
    mutate(Pr = factor(Pr), percentage = n / sum(n)) %>% 
    ggplot(aes(x = "", percentage, fill = Pr)) +
    geom_col(color="white") +
    coord_polar(theta = "y", start=0) +
    theme_void()

Created on 2020-01-13 by the reprex package (v0.3.0.9000)

Thanks but I have an error with the code:

Program <- dataset$Pr
pie_chart <-
count(Program) %>%
mutate(Program = factor(Pr), percentage = n / sum(n)) %>%
ggplot(aes(x = "", percentage, fill = Program)) +
geom_col(color="white") +
coord_polar(theta = "y", start=0) +
theme_void()

and I have this error:
Error in UseMethod("summarise_") :
no applicable method for 'summarise_' applied to an object of class "c('double', 'numeric')"

You are using the count() function wrong, it doesn't take a vector as an argument, you have to pass a data frame and a variable name, like in the example I gave you before with your own data.

Perfect thanks
In order to insert also the % labels for each slide of the pie how can I do?
For each slide, I need the count of the ID for each program.

Thanks again
Giada

The percentage values have already been calculated in the percentage variable so you just have to add the labels to the plot, take a look at the geom_text() or geom_label() functions.

If I insert the geom(label), I obtain the screenshot attached that is not correct I suppose.
How can I obtain a label for each slice?
Thanks!

The code is:
new_dataframe %>%
count(Pr) %>%
mutate(Pr = factor(Pr), percentage = n / sum(n)) %>%
ggplot(aes(x = "", percentage, fill = Pr)) +
geom_col(color="white") +
coord_polar(theta = "y", start=0) +
labs(title = "pie chart") +
geom_label(
label="Pr",
x=4.1,
y=20,
label.padding = unit(0.55, "lines"),
label.size = 0.35,
color = "black",
fill="#69b3a2"
)
theme_void()

Have you read the documentation for the function? you are supposed to map the percentage variable to the label aesthetic inside an aes() function, so you are using the wrong syntax.

library(tidyverse)

sample_data <- read_delim("https://www.dropbox.com/s/ma5wqmegy6ud8q1/dataset%20example.csv?dl=1", delim = ";")

sample_data %>%
    count(Pr) %>%
    arrange(desc(Pr)) %>%
    mutate(Pr = factor(Pr),
           percentage = n / sum(n)) %>% 
    drop_na(Pr) %>% 
    ggplot(aes(x = "", percentage, fill = Pr)) +
    geom_col(color="white") +
    coord_polar(theta = "y", start=0) +
    labs(title = "Pie Chart") +
    geom_label(aes(label = scales::percent(percentage), x = 1.3),
               position = position_stack(vjust = 0.5),
               color = "black",
               fill="#69b3a2") +
    theme_void()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.