• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Analysis Factor

The Analysis Factor

Statistical Consulting, Resources, and Statistics Workshops for Researchers

  • Home
  • Our Programs
    • Membership
    • Online Workshops
    • Free Webinars
    • Consulting Services
  • About
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Collaborate with Us
  • Statistical Resources
  • Contact
  • Blog
  • Login

R Is Not So Hard! A Tutorial, Part 14: Pie Charts

by guest contributer 4 Comments

by David Lillis, Ph.D.

In Part 14, let’s see how to create pie charts in R. Let’s create a simple pie chart using the pie() command. As always, we set up a vector of numbers and then we plot them.

B <- c(2, 4, 5, 7, 12, 14, 16)

Create a simple pie chart.

pie(B)

image001

Now let’s create a pie chart with a heading, using nice colours, and define our own labels using R’s rainbow palette. We control the number of colours using length(B).

B <- c(2, 4, 5, 7, 12, 14, 16)
pie(B, main="My Piechart", col=rainbow(length(B)),
labels=c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"))

image002

Here is a more complex example, using percentages and a legend. We create a vector of data, one for each day of the week

B <- c(5, 3, 1, 8, 9, 4, 6)

Set up black, grey and white for clear printing.

cols <- c("grey90","grey50","black","grey30","white","grey70","grey50")

Calculate the percentage for each day, using one decimal place.

percentlabels<- round(100*B/sum(B), 1)

Add a ‘%’ sign to each percentage value using the paste command.

pielabels<- paste(percentlabels, "%", sep="")

What does the paste command do?

pie(B, main="My Best Piechart", col=cols, labels=pielabels, cex=0.8)

Create a legend at the right.

legend("topright", c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"), cex=0.8, fill=cols)

Here is your pie chart:
image003

OK. Now let’s create a pie chart from a data frame and include sample sizes. First create a table of counts of cylinder numbers from the mtcars data set.

cyltable<- table(mtcars$cyl)
cyltable
4 6 8
11 7 14

We have eleven cars with four cylinders, seven cars with six cylinders, and fourteen cars with eight cylinders.

Now we create labels.

labs<- paste("(",names(cyltable),")", "\n", cyltable, sep="")

Now we plot.

pie(cyltable, labels = labs, col = c("red", "yellow", "blue"),
main="PIE CHART OF CYLINDER NUMBERS\n with sample sizes")

image004

That wasn’t so hard! In Part 15 we will look at further plotting techniques in R.

About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.

Bookmark and Share

Tagged With: graphics, pie chart, R

Related Posts

  • R Is Not So Hard! A Tutorial, Part 13: Box Plots
  • R is Not So Hard! A Tutorial, Part 12: Creating Histograms & Setting Bin Widths
  • Member Training: What’s the Best Statistical Package for You?
  • The Advantages of RStudio

Reader Interactions

Comments

  1. Mark says

    May 22, 2018 at 12:58 am

    How do you get the labels inside the pie chart instead of outside?

    Reply
  2. Preethishetty says

    March 26, 2018 at 1:35 am

    Yeah, after reading this article I realized that it is easy to learn R. Thanks for posting such types of articles and motivating us to learn R Language.

    Reply
  3. Danielle says

    October 16, 2016 at 6:04 pm

    How do you combine the names (labels) and the percentages (relative frequencies) on the graphs?

    Reply
  4. Gordon says

    August 26, 2016 at 7:32 am

    Awesome instruction. Real easy and effective to see how R works. I pasted your coding into Wordpad, cleaned out the notes, and pasted it straight into R and saw instantaneous results. Beautiful.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.

Primary Sidebar

This Month’s Statistically Speaking Live Training

  • Member Training: Analyzing Pre-Post Data

Upcoming Free Webinars

Poisson and Negative Binomial Regression Models for Count Data

Upcoming Workshops

  • Analyzing Count Data: Poisson, Negative Binomial, and Other Essential Models (Jul 2022)
  • Introduction to Generalized Linear Mixed Models (Jul 2022)

Copyright © 2008–2022 The Analysis Factor, LLC. All rights reserved.
877-272-8096   Contact Us

The Analysis Factor uses cookies to ensure that we give you the best experience of our website. If you continue we assume that you consent to receive cookies on all websites from The Analysis Factor.
Continue Privacy Policy
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT