Create a Bar Chart in R

Your goal

You need to create a bar 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 bar chart.

Step-by-step tutorial

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

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

You can also attach categories to the numerical values:

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

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

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