R Plots - Pie Charts
R provides a large number of libraries for plotting.
A pie chart, also called a pie graph, is a circular statistical chart divided into several sectors, used to describe the relative relationships among quantities, frequencies, or percentages.
R uses the pie() function to create pie charts; the syntax is 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: A numeric vector representing the area of each sector.
- labels: A character vector giving the labels for each sector's area.
- edges: This parameter is of little use; it refers to 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 clockwise.
- angle: Sets the slope of the shading lines.
- density: The density of the shading. The default value is NULL.
- col: The color of each sector, equivalent to a palette.
To draw a pie chart, you need to prepare: a vector reflecting the quantities, labels for each part, and the colors of each part (optional).
Next, we draw a simple pie chart:
Example
Data preparation
info = c
(
1
,
2
,
4
,
8
)
Naming
names = c
(
"Google"
,
"Runoob"
,
"Taobao"
,
"Weibo"
)
Coloring (optional)
cols = c
(
"#ED1C24"
,
"#22B14C"
,
"#FFC90E"
,
"#3f48CC"
)
Plotting
pie
(
info,
labels
=names,
col
=cols
)
Running the plotting program creates a PDF file (Rplots.pdf) in the current directory; opening the file shows the following graphic:

We can also use the png(), jpeg(), and bmp() functions to set the output file format to an image:
Example
Data preparation
info
=
c
(
1
,
2
,
4
,
8
)
Naming
names
=
c
(
"Google"
,
"Runoob"
,
"Taobao"
,
"Weibo"
)
Coloring (optional)
cols
=
c
(
"#ED1C24"
,
"#22B14C"
,
"#FFC90E"
,
"#3f48CC"
)
Set the output image
png
(
file
=
'runoob-pie.png'
, height
=
300
, width
=
300
)
Plotting
pie
(
info,
labels
=
names
,
col
=
cols
)
Next, we set a title for the pie chart. For Chinese fonts, you need to set the font parameter family='GB1'; you can also set your own font library. For details, refer to:R Plotting - Chinese Support。
Example
Data preparation
info = c
(
1
,
2
,
4
,
8
)
Naming
names = c
(
"Google"
,
"Runoob"
,
"Taobao"
,
"Weibo"
)
Coloring (optional)
cols = c
(
"#ED1C24"
,
"#22B14C"
,
"#FFC90E"
,
"#3f48CC"
)
Calculate percentages
piepercent = paste
(
round
(
100
info
/
sum
(
info
)
)
,
"%"
)
Plotting
pie
(
info,
labels
=piepercent, main =
"Website Analytics"
,
col
=cols,
family
=
'GB1'
)
Add a color sample annotation
legend
(
"topright"
, names,
cex
=
0.8
,
fill
=cols
)

To draw a 3D pie chart, you can use the pie3D() function of the plotrix library; before using it, we need to install it:
install.packages("plotrix", repos = "https://mirrors.ustc.edu.cn/CRAN/")
Example
Load plotrix
library
(
plotrix
)
Data preparation
info
=
c
(
1
,
2
,
4
,
8
)
Naming
names
=
c
(
"Google"
,
"Runoob"
,
"Taobao"
,
"Weibo"
)
Coloring (optional)
cols
=
c
(
"#ED1C24"
,
"#22B14C"
,
"#FFC90E"
,
"#3f48CC"
)
Set the filename and output as png
png
(
file
=
"3d_pie_chart.png"
)
Draw the 3D chart; family should be set to a Chinese font library supported by your system
pie3D
(
info,
labels
=
names
,explode
=
0.1
, main
=
"3D Chart"
,
family
=
"STHeitiTC-Light"
)
The generated image is shown below:
