Create a Pie Chart in R

Your goal

You need to create a pie chart for numerical data in R.

If you have only categorical data and you need to generate counts, you will need to create a frequency table before you can create the pie chart.

Step-by-step tutorial

You can create a pie chart from a vector of numerical data using pie:

> data <- c(13, 11, 17, 9, 4, 14)
> pie(data)
A pie chart

You can also attach categories to the numerical values:

> barplot(data, names.arg=c('dog', 'cat', 'mouse', 'frog', 'fish', 'pig'))
A pie chart with categories

If you have categorical data, you can convert them to a frequency table and then create a pie chart for the frequency table:

> fruits <- c('apple', 'apple', 'banana', 'cherry', 'cherry', 'cherry', 'pear')
> fruits.freqs <- table(fruits)
> fruits.freqs
fruits
 apple banana cherry   pear
     2      1      3      1
> pie(fruits.freqs)
A pie chart based on a frequency table