English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Plot - Pie Chart

R provides a large number of libraries to implement drawing functions.

A pie chart, also known as a pie-shaped chart, is a circular statistical chart divided into several sectors, used to describe the relative relationships between quantities, frequencies, or percentages.

R uses the pie() function to implement pie charts, with the syntax format as follows:

pie(x, labels = names(x), edges = 200, radius = 0.8,
    clockwise = FALSE, init.angle = if(clockwise) 90 else 0,
    density = NULL, angle = 45, col = NULL, border = NULL,
    lty = NULL, main = NULL, ...)
  • x: Numeric vector, representing the area of each sector.

  • labels: Character vector, representing the area labels of each sector.

  • edges: This parameter is not very useful, indicating the number of sides of the polygon (the outline of a circle is similar to a polygon with many sides).

  • radius: The radius of the pie chart.

  • main: The title of the pie chart.

  • clockwise: A logical value indicating whether the slices of the pie chart are divided in a clockwise direction.

  • angle: Sets the slope of the texture.

  • density: The density of the texture. The default value is NULL.

  • col: Represents the color of each sector, which is equivalent to a palette.

To draw a pie chart, you need to prepare these: a vector reflecting the quantity, labels for each part, and colors for each part (optional).

Next, let's draw a simple pie chart:

# Data Preparation
info = c(1, 2, 4, 8)
# Naming
names = c("Google", "w3codebox", "Taobao", "Weibo")
# Coloring (optional)
cols = c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Plotting
pie(info, labels=names, col=cols)

Executing the plotting program will generate a PDF file (Rplots.pdf) in the current directory. Open the file to see the graphic effect as follows:

We can also use png(), jpeg(), bmp() functions to set the output file format to image:

# Data Preparation
info = c(1, 2, 4, 8)
# Naming
names = c("Google", "w3codebox", "Taobao", "Weibo")
# Coloring (optional)
cols = c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Set output image
png(file='w3codebox-pie.png', height=300, width=300)
# Plotting
pie(info, labels=names, col=cols)

Next, we set the title for the pie chart, the Chinese font needs to set the font parameter family='GB'1You can also set your own font library, for details, please refer to:R Plotting - Chinese Support

# Data Preparation
info = c(1, 2, 4, 8)
# Naming
names = c("Google", "w3codebox", "Taobao", "Weibo")
# Coloring (optional)
cols = c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Calculate Percentage
piepercent = paste(round(100*info/sum(info)), "%")
# Plotting
pie(info, labels=piepercent, main = "Website Analysis", col=cols, family='GB1')
# Add color sample annotation
legend("topright", names, cex=0.8, fill=cols)

If you want to draw 3D pie chart, you can use the pie function of the plotrix library3D() function, we need to install it first before using:

install.packages("plotrix", repos = "https://mirrors.ustc.edu.cn/CRAN/)
# Load plotrix
library(plotrix)
# Data Preparation
info = c(1, 2, 4, 8)
# Naming
names = c("Google", "w3codebox", "Taobao", "Weibo")
# Coloring (optional)
cols = c("#ED1C24","#22B14C","#FFC90E","#3f48CC")
# Set file name, output as png
png(file = "3d_pie_chart.png")
# Drawing 3D 图,family to set the Chinese font library supported by your system
pie3D(info,labels = names,explode = 0.1, main = "3D 图,family = "STHeitiTC"-Light")

The generated image is as follows: