O'Reilly logo

R Graph Essentials by David Alexander Lillis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating bar charts

Let's see how to create bar charts in R. We will create a simple bar chart using the barplot() command, which is easy to use. First, we set up a vector of numbers. Then we count them using the table() command and plot the counts. The table() command creates a simple table of counts of the elements in a dataset. Enter the following vector into R:

H <- c(2,3,3,3,4,5,5,5,5,6)

Now, we count each element using the table() command as follows:

counts <- table(H)
counts

The output is as follows:

H
2 3 4 5 6 
1 3 1 4 1    

Now we plot the counts.

barplot(counts)

Here is the bar chart:

Creating bar charts

The horizontal axis records the elements in your dataset, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required