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

R pie() Function - Drawing Pie Charts

R pie() Function - Drawing Pie Charts

R Language Examples R Language Examples

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:

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 R Language Examples