R pie() Function - Drawing Pie Charts
The R pie() function is used to draw pie charts, showing the proportion of each part in the whole.
Pie charts are suitable for showing proportions of no more than 5-6 categories.
The syntax of the pie() function is as follows:
pie(x, labels = names(x), radius = 0.8,
main = "", col = NULL, clockwise = FALSE)
Parameter Description:
-
x A numeric vector for each part.
-
labels Labels for each part.
-
radius Pie radius (0-1), 0.8 by default.
Example
Market share data
market_share
<-
c
(
35
,
28
,
20
,
12
,
5
)
names
(
market_share
)
<-
c
(
"Company A"
,
"Company B"
,
"Company C"
,
"Company D"
,
"other"
)
Calculate percentages
pct
<-
round
(
market_share
/
sum
(
market_share
)
100
,
1
)
labels
<-
paste
(
names
(
market_share
)
,
"\n"
, pct,
"%"
, sep
=
""
)
Draw the pie chart
pie
(
market_share,
labels
=
labels
,
main
=
"Market Share Distribution"
,
col
=
c
(
"steelblue"
,
"coral"
,
"seagreen"
,
"gold"
,
"gray"
)
)
Running the above code displays a pie chart of each company's market share with percentage labels.
R Language Examples