Plotting Pie Charts in Ggplot2

Plotting pie charts in ggplot2

We can first calculate the percentage of each cut group. I used the dplyr package for this task.

library(ggplot2)
library(dplyr)

# Calculate the percentage of each group
diamonds_summary <- diamonds %>%
group_by(cut) %>%
summarise(Percent = n()/nrow(.) * 100)

After that, we can plot the pie chart. scale_y_continuous(breaks = round(cumsum(rev(diamonds_summary$Percent)), 1)) is to set the axis label based on cumulative percentage.

ggplot(data = diamonds_summary, mapping = aes(x = "", y = Percent, fill = cut)) + 
geom_bar(width = 1, stat = "identity") +
scale_y_continuous(breaks = round(cumsum(rev(diamonds_summary$Percent)), 1)) +
coord_polar("y", start = 0)

Here is the result.

Sample Image

ggplot how to plot pie charts on USA map with state's id

Using the usmap and the scatterpie packages this could be achieved via ggplot2 like so:

  1. Add coordinates for the pies to your data. In the code below I use the coordinates of the state centers provided by usmapdata::centroid_labels
  2. Add the pies from your data via geom_scatterpie
library(usmap)
library(ggplot2)
library(scatterpie)

states <- us_map("states")

centroids <- usmapdata::centroid_labels("states")[c("x", "y", "abbr")]

data <- merge(data, centroids, by.x = "region", by.y = "abbr", all.x = TRUE)

plot_usmap(regions = "states") +
geom_scatterpie(aes(x, y, group = region),
data = data, cols = c("gas", "coal", "wind", "solar")
) +
geom_text(aes(x, y, label = region),
data = data, vjust = 1, nudge_y = -100000
)

Sample Image

EDIT If you want to exclude some states (or include only some states) you could do so via the exclude or include argument of plot_usmap:

plot_usmap(regions = "states", exclude = c("AK", "HI")) +
geom_scatterpie(aes(x, y, group = region),
data = data, cols = c("gas", "coal", "wind", "solar")
) +
geom_text(aes(x, y, label = region),
data = data, vjust = 1, nudge_y = -100000
)

Sample Image

fancy pie chart in R using ggplot2

You could do:

ggplot(data, aes(x="", y=prop, fill=group)) +
geom_bar(stat="identity", width=10, size = 3, color = "white") +
coord_polar("y", start=0) +
theme_void() +
theme(legend.position="none") +
geom_text(aes(y = ypos, label = paste(group, round(prop,2), sep = "\n")),
color = "white", size=4, nudge_x = 3) +
scale_fill_brewer(palette="Set1")

Sample Image

In ggplot2, how to add a white hole in the middle of the pie chart

Just widen the limits of your x axis (it's easier to do this if you don't convert the year into a factor):

pie_data %>% ggplot(aes(x = year, y = sales, fill = category))+
geom_col(position = 'fill', width = 1, color = 'white') +
coord_polar(theta = 'y') +
lims(x = c(2019, 2022)) +
theme_void()

Sample Image

You can control the size of the white hole by changing 2019 in the above code. The earlier the year, the larger the hole:

pie_data %>% ggplot(aes(x = year, y = sales, fill = category))+
geom_col(position = 'fill', width = 1, color = 'white') +
coord_polar(theta = 'y') +
lims(x = c(2017, 2022)) +
theme_void()

Sample Image



Related Topics



Leave a reply



Submit