
GGPLOT2 PIE CHART WITH PERCENTAGE CODE
As you calculate where to place the labels based on the ordering in your data frame, this works out wrong.Īs a general principle of readability, do all the fancy calculations of labels and positions they go before the actual code drawing the graphic. your problem comes from the order in which the wedges are drawn, which will default to alphabetical. I agree with a waffle chart would be better. y y Right: The themevoid 'finalizes' the pie chart ggplot(data, Percentage. Names = c("Group", "value"), class = "ame", row.names = c("1", We need the polar coordinate system to make a pie chart and we must. Guides(fill = guide_legend(title = "Group"))ĭATA mydf <- structure(list(Group = structure(c(3L, 1L, 2L). Geom_label_repel(aes(label = prop), size=5, show.legend = F, nudge_x = 1) + Pie <- ggplot(mydf, aes(x = "", y = value, fill = fct_inorder(Group))) +

Mutate(prop = percent(value / sum(value))) -> mydf When geom_label_repel() added labels to the pie, the order of label was identical to that of the pie. When I drew the ggplot figure, I specified the order of Group in the order in mydf (i.e., Negative, Positive, and Neutral) using fct_inorder(). I also calculated the percentage in advance. I sorted the data in descending order by value. I can now use this function for any other projects we work on.Here is an idea matching the order of groups in the pie chart and the order of labels. Here’s a video walkthrough with the code used in the video below that.Īnd there we have it: a donut chart with a big number in the middle. Create our data frameįirst, we create our data frame. I’ve written code and recorded videos to show each step. Let’s break apart this function piece by piece in order to see how it works. highlight_color sets the color of the donut chart and the big number.font_family to use any font for the big number (of course, you have to have the font installed in order for it to work).For this function, I assume it’s a percentage (you could adjust the function for non-percentages). value is the number (from 0 to 1) that we want to highlight.

In order to use the function, we’d write code like this. # Add the big number in the center of the hole # Set theme_void() to remove grid lines and everything else from the plot Scale_fill_manual(values = c(highlight_color, "grey90")) + # Set a color scale with the highlighted section in whatever color # is chosen with the highlight_color argument and the rest in a light gray # Set the limits, which is important for adding the hole # A pie/donut chart is a bar chart with polar coordinates # Add polar coordinates and set the direction to -1 # so the filled in part starts at the top and goes clockwise # Create a nicely formatted big number to go in the donut holeīig_number_text_label <- percent(value, accuracy = 1)
GGPLOT2 PIE CHART WITH PERCENTAGE FULL
Next, let me show you the full function with comments throughout. I only need two packages: the tidyverse (which loads ggplot2 and the dplyr package, which I use for data wrangling) and the scales package, which I use to make the nicely formatted number in the hole of the donut. Creating a Function to Make a Donut Chart with Big Number Function in ggplot2 We’ve made donut charts with big numbers regularly enough that I decided it was time to develop a function for it. Rather than just writing, “65% of people did X,” we make something like this: In our consulting work, we often combine donut charts and big numbers.

There are two techniques we often use in our consulting work to show single data points: But what if you want to show just one data point? Say, for example, you want to show what percentage of people have accomplished something?

A lot of data visualizations are designed to show multiple data points (think bar charts that compare results for multiple groups).
