pie3D function in R

Package

plotrix

Author

Jim Lemon

Basic 3D pie chart

3D pie charts are not recommended, but if you really want to create them you can use pie3D from plotrix package. The default 3D pie chart will look like the following.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data)

pie3D in R from plotrix package

Radius (width)

The radius argument allows modifying the radius of the pie in user units. Default value is 1.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      radius = 0.75)

Radius of pie3D

Height

Similarly, you can change the height of the pie with height. Default value is 0.1

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      height = 0.2)

Height of the 3D pie

Angle

You can also change the viewing angle with theta. Default value is pi/6.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      theta = 1.5)

Visualization angle in pie3D

pie3D color

pie3D color palettes in R

The color of the pie slices can be customized with col. In the following examples we are using the "Spectral" palette, generating as many colors as slices.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      col = hcl.colors(length(data), "Spectral"))

3D pie border color in R

The border color can also be customized with border argument. In the following block of code we are setting a white border.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      col = hcl.colors(length(data), "Spectral"),
      border = "white")

pie3D shade effect

Finally, you can change the shade of the border of the pie, in order to modify the 3D effect. This will darker or lighter the corresponding colors.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      col = hcl.colors(length(data), "Spectral"),
      shade = 0.5)

pie3D labels

You can add labels to the pie representing the value, label or percentage for each slice passing a vector of the same size as the number of slices to labels argument.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      col = hcl.colors(length(data), "Spectral"),
      labels = data)

Adding labels to pie3D function

If you have a numeric vector that doesn’t represent percentages consider transforming the data into percentage as in this example.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)
lab <- paste0(round(data/sum(data) * 100, 2), "%")

pie3D(data,
      col = hcl.colors(length(data), "Spectral"),
      labels = lab)

pie 3D with percentages in R

The color and size of the labels can be customized with labelcol and labelcex arguments. The latter defaults to 1.5.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data,
      col = hcl.colors(length(data), "Spectral"),
      labels = data,
      labelcol = "red",
      labelcex = 0.75)

R pie3D labels color and size

Explode pie

pie3D explode

You can also “explode” the pie with explode.

# install.packages("plotrix")
library(plotrix)

data <- c(19, 21, 54, 12, 36, 12)

pie3D(data, mar = rep(1.75, 4),
      col = hcl.colors(length(data), "Spectral"),
      labels = data,
      explode = 0.2)
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon
ggplot2

Elegant Graphics for Data Analysis

Buy on Amazon

See also