

To get started, you need a set of data to work with. Type of lines used for plotting pie chart If True, slices are drawn clockwise ortherwise counter-clockwiseĪ vector of colors to be used in filling or shading the slices Pie( clockwise, init.angle, labels, density, angle, col, border, lty, main, …) Parameters R pie() function parameters Parameter It has many options and arguments to control many things, such as labels, titles and colors. In R, you can create a pie chart using the pie() function.
#GGPLOT2 PIE CHART WITH PERCENTAGE SERIES#
Legend.title = element_text(hjust = 0.They are good if you are trying to compare parts of a single data series to the whole.

Ggplot(table_labels, aes(x = "", y = Count, fill = Food)) +
#GGPLOT2 PIE CHART WITH PERCENTAGE CODE#
Running the ggplot() function with the add-on code gives us a nice pie chart. We can combine values and strings together with paste0(). The paste0() function is R’s version of concatenation. Table_labels # Food Count cumulative midpoint labels Labels = paste0(round((Count/ sum(Count)) * 100, 1), "%", " (", Count, ") ")) Adding Percentage And Count Labels To The Pie Chart Using ggplot2. # Pie Chart With Percentage & Counts Labels: The code here is very similar to the previous code but the line with paste0() is modified such that the counts are included. Geom_text(aes(x = 1.2, y = midpoint, label = labels), color="black",Īdding Percentage And Count Labels To The Pie Chart Using ggplot2 Ggplot(table_percent, aes(x = "", y = Count, fill = Food)) + # ggplot Pie Chart with percentage labels

The line with geom_text() enables the percentage labels onto the pie chart. Now the ggplot() command along with the corresponding code can be used. Table_percent # Food Count cumulative midpoint labels These labels will come out as percentages on the pie chart. The next lines of code will convert the values under the Food column as factors and add label positions based on cumulative counts and midpoints. Library(dplyr) #Data Wrangling and Manipulation The dplyr package for data manipulation and data wrangling is loaded into R. Adding the percentage labels takes a bit of work here but it is manageable. The pie chart above is very nice but it could use percentage labels. The theme() functions allows for customization of the appearance of the title.Īdding Percentage Labels To The Pie Chart Using ggplot2 The labs() function allows for customization of the title and labels. Legend.title = element_text(hjust = 0.5, face="bold", size = 10))įrom the previous code, scale_fill_manual() allows for colour choices in the pieces of the pie chart. hcgen <- ggplot (hc, aes (x factor (1), fill gender)) Then I use a blank theme to visualize the chart. Theme(plot.title = element_text(hjust = 0.5), I use ggplot to build a pie chart based on a factor variable in a data frame.

Labs(x = "", y = "", title = "Favourite Food Survey \n", Scale_fill_manual(values = c("Blue", "Red", "Green", "Orange")) + The main line of code is coord_polar(theta = "y", start = 0) which will convert the bar graph into a pie (circular) graph. To create the actual pie chart more code is needed on top of the existing code for the bar graph. Ggplot(table, aes(x = "", y = Count, fill = Food)) + This next lines of codes shows why a bar graph is not that good for displaying this data. In ggplot2, the pie chart follows from the bar graph. Load the ggplot2 package using this code below. ggplot2 allows R users to create pie charts, bar graphs, scatter plots, regression lines and more. The ggplot2 package in R is very good for data visuals. (Add from top to bottom of second column in table.) One could compute the total number of people in this food survey. The column names in the table can be changed as well by using colnames(). # $ food_choices: Factor w/ 4 levels "Caesar Salad".: 3 2 4 1 # 4 Caesar Salad 11 # Check structure of table: You can check the contents of the table by typing out table and/or using the str() command in R. Table <- ame(food_choices, counts) # Create data frame The food choices are Pizza, Pasta, Sushi, and Caesar Salad.įood_choices <- c("Pizza", "Pasta", "Sushi", "Caesar Salad") An alternative to display percentages on the pie chart is to use the PieChart function of the lessR package, that shows the percentages in the middle of the. This data table will contain favourite food choices and their counts from a survey. For the pie chart, I am creating a fake sample dataset.
