Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Plots - Pie Charts

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, …)

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: