Beginners Guide to R – R Pie Chart – Base Graph

Hits: 16

 

R Pie Chart – Base Graph

A Pie Chart is a special chart that shows relative sizes of data using pie slices.

typical pie chart

They are good if you are trying to compare parts of a single data series to the whole.

The pie() function

In R, you can create a pie chart using the pie() function.

It has many options and arguments to control many things, such as labels, titles and colors.

Syntax

The syntax for the pie() function is:

pie(clockwise,init.angle,labels,density,angle,col,border,lty,main,)

Parameters

Parameter Description
clockwise If True, slices are drawn clockwise ortherwise counter-clockwise
init.angle The starting angle for the slices
labels The names for the slices
density The density of shading lines
angle The slope of shading lines
col A vector of colors to be used in filling or shading the slices
border The color to be used for the border
lty Type of lines used for plotting pie chart
main An overall title for the plot
Other graphical parameters

Create a Simple pie chart

To get started, you need a set of data to work with. Let’s consider a survey was conducted of a group of 190 individuals, who were asked “What’s your favorite fruit?”.

The result might appear as follows:

Fruit: Apple Kiwi Grapes Banana Pears Orange
People: 40 15 30 50 20 35

Let’s put this data in a vector.

survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
survey
 apple   kiwi  grape banana   pear orange 
    40     15     30     50     20     35 

To create a pie chart just specify the vector in pie() function.

# Create a pie chart from a vector of data points
pie(survey)

It is really a good way to show relative sizes: you can see which fruits are most liked, and which are not, at a glance.

Coloring a Pie Chart

You can change the colors of each pie slice by passing a vector of colors to the col argument.

# Change the colors of each pie slice
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
    col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"))
# Create a shaded pie chart
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
    col=gray(seq(0.4, 1.0, length = 6)))

Labeling a Pie Chart with Percentage

Often you want to label each pie slice with the percentage of the whole that slice represents. You can do that by passing the precalculated percent values to the labels argument.

survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pct <- round(survey/sum(survey)*100)		# calculate percentages
lbls <- paste(names(survey), pct, "%")	# add percents to labels
pie(survey,
    col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"),
    labels=lbls)

If this argument is omitted, then the labels are taken from the names attribute of a vector.

Adding Hatches

Adding hatches to each pie slice is rather easy, just specify the density argument in the pie()function.

By default the chart is hatched with 45° slanting lines, however, you can change it with the angle argument.

# Create a hatched pie chart with different slanting lines
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
    col="steelblue",
    density = 20,
    angle = 30*1:6)

Pie Chart Start Angle and Direction

Use the init.angle and clockwise arguments to set the starting angle for the first segment in a pie chart, and the direction of the segments (clockwise or counter-clockwise).

By default, the init.angle is 0° (3 o’clock) and the direction of the segments is counter-clockwise.

If you change the direction of the segments to clockwise, the init.angle defaults to 90° (12 o’clock).

# Change the start angle to 90° and the direction of the segments to clockwise
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie(survey,
    col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"),
    init.angle = 90,
    clockwise = TRUE)

3D Pie Chart

To create a 3D pie chart, use pie3D() function of plotrix package and pass in the vector of data points.

You can alter the appearance of your 3D pie chart by using following parameters.

Parameter Description
col A vector of colors to be used in filling slices
main An overall title for the plot
labels The names for the slices
labelcex The character expansion factor for the labels
explode The amount to explode the pie
theta The angle of viewing in radians
edges The number of lines forming an ellipse
radius The radius of the pie
height The height of the pie
start The angle at which to start drawing sectors
border The color of the sector border lines
library(plotrix)
survey <- c(apple=40, kiwi=15, grape=30, banana=50, pear=20, orange=35)
pie3D(survey,
      col=c("steelblue4", "steelblue", "steelblue3", "steelblue2", "steelblue1", "skyblue1"),
      labels = names(survey),
      labelcex = 1,
      explode=0.1,
      theta = 0.8,
      main="3D Pie Chart")

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.