ggplot2 pie chart : Quick start guide - R software and data visualization


This R tutorial describes how to create a pie chart for data visualization using R software and ggplot2 package.

The function coord_polar() is used to produce a pie chart, which is just a stacked bar chart in polar coordinates.

Simple pie charts

Create some data :

df <- data.frame(
  group = c("Male", "Female", "Child"),
  value = c(25, 25, 50)
  )
head(df)
##    group value
## 1   Male    25
## 2 Female    25
## 3  Child    50

Use a barplot to visualize the data :

library(ggplot2)
# Barplot
bp<- ggplot(df, aes(x="", y=value, fill=group))+
geom_bar(width = 1, stat = "identity")
bp

ggplot2 pie chart for data visualization in R software

Create a pie chart :

pie <- bp + coord_polar("y", start=0)
pie

ggplot2 pie chart for data visualization in R software

Change the pie chart fill colors

It is possible to change manually the pie chart fill colors using the functions :

  • scale_fill_manual() : to use custom colors
  • scale_fill_brewer() : to use color palettes from RColorBrewer package
  • scale_fill_grey() : to use grey color palettes
# Use custom color palettes
pie + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

ggplot2 pie chart for data visualization in R software

# use brewer color palettes
pie + scale_fill_brewer(palette="Dark2")

ggplot2 pie chart for data visualization in R software

pie + scale_fill_brewer(palette="Blues")+
  theme_minimal()

ggplot2 pie chart for data visualization in R software

# Use grey scale
pie + scale_fill_grey() + theme_minimal()

ggplot2 pie chart for data visualization in R software

Read more on ggplot2 colors here : ggplot2 colors

Create a pie chart from a factor variable

PlantGrowth data is used :

head(PlantGrowth)
##   weight group
## 1   4.17  ctrl
## 2   5.58  ctrl
## 3   5.18  ctrl
## 4   6.11  ctrl
## 5   4.50  ctrl
## 6   4.61  ctrl

Create the pie chart of the count of observations in each group :

ggplot(PlantGrowth, aes(x=factor(1), fill=group))+
  geom_bar(width = 1)+
  coord_polar("y")

ggplot2 pie chart for data visualization in R software

Customized pie charts

Create a blank theme :

blank_theme <- theme_minimal()+
  theme(
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  panel.border = element_blank(),
  panel.grid=element_blank(),
  axis.ticks = element_blank(),
  plot.title=element_text(size=14, face="bold")
  )
  1. Apply the blank theme
  2. Remove axis tick mark labels
  3. Add text annotations : The package scales is used to format the labels in percent
# Apply blank theme
library(scales)
pie + scale_fill_grey() +  blank_theme +
  theme(axis.text.x=element_blank()) +
  geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]), 
            label = percent(value/100)), size=5)

ggplot2 pie chart for data visualization in R software

# Use brewer palette
pie + scale_fill_brewer("Blues") + blank_theme +
  theme(axis.text.x=element_blank())+
  geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]), 
                label = percent(value/100)), size=5)

ggplot2 pie chart for data visualization in R software

Infos

This analysis has been performed using R software (ver. 3.1.2) and ggplot2 (ver. 1.0.0)


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!






This page has been seen 92234 times