R Pie Chart

Pie chart is drawn using the pie() function in R programming . This function takes in a vector of non-negative numbers.

expenditure<-c("Housing"=600,"Food"=300,"Cloths"=150,"Entertainment"=100,"Other"=200)
expenditure
##       Housing          Food        Cloths Entertainment         Other 
##           600           300           150           100           200

Let us consider the above data represents the monthly expenditure breakdown of an individual

Example: Simple pie chart using pie()

Now let us draw a simple pie chart out of this data using the pie() function

pie(expenditure)

Example 2: Pie chart with additional parameters

We can pass in additional parameters to affect the way pie chart is drawn. You can read about it in the help section ?pie.

?pie
## starting httpd help server ... done

Some of the frequently used ones are, labels-to give names to slices, main-to add a title, col-to define colors for the slices and border-to color the borders. We can also pass the argument clockwise=TRUE to draw the chart in clockwise fashion.

pie(expenditure,
    labels=as.character(expenditure),
    main="Monthly Expenditure Breakdown",
    col=c("red","orange","yellow","blue","green"),
    border="brown",
    clockwise=TRUE
)

To get names of each value

pie(expenditure,
    labels=names(expenditure),
    main="Monthly Expenditure Breakdown",
    col=c("red","orange","yellow","blue","green"),
    border="brown",
    clockwise=TRUE
)