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.
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:
- 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
- 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
)
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
)
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")
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()
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()
Related Topics
How to Draw a Contour Plot When Data Are Not on a Regular Grid
How to Prevent User from Setting the End Date Before the Start Date Using the Shiny Daterangeinput
Highlight Areas Within Certain X Range in Ggplot2
Different Y-Axis Labels Facet_Grid and Sizes
Split Concatenated Column to Corresponding Column Positions
R Dataframe with Varied Column Lengths
R - Set Execution Time Limit in Loop
Apply a Summarise Condition to a Range of Columns When Using Dplyr Group_By
Find All Sequences with the Same Column Value
Using Data.Table to Create a Column of Regression Coefficients
How to Select_If in Dplyr, Where the Logical Condition Is Negated
Avoid Copying the Whole Vector When Replacing an Element (A[1] <- 2)