Bar charts represent data in the form of rectangular bars. In R, we can create bar charts using barplot() function. We can supply a matrix or vector to this function.  R supports both vertical and horizontal bars in the bar chart.



H is a vector or matrix of numeric values used in the bar chart

xlab x-axis label

ylab y-axis label

main title of the bar chart

names.arg is a vector of names appearing under each bar

col is the colors for the bars

Example 1:

Let’s create a simple vertical bar chart using just the input vector and the name of the each bar. The below script will create and save the bar chart in current working directory of R.


R Bar Charts 1











Example 2:

Let’s create simple horizontal bar chart:


R Bar Charts 2












Bar Chart Labels, Title and Colors

We can expand the features of the bar chart can be expanded by adding more parameters.



R Bar Charts 3















Group Bar Chart and Stacked Bar Chart

We can also create a bar chart with groups and stacks using a matrix as input values. More than two variables are represented as a matrix which is used to create the group bar chart and stacked bar chart.



R Bar Charts 4














Plotting Categorical Data

Sometimes we have to plot the count of each item as bar plots from categorical data. For example, here is a vector of the age of 10 college students.


Simply doing barplot(age) will not give us the required plot. It simple plot 10 bars with height equal to the age of the student. But we want to know the number of student in each age category.

This count can be easily found using the table() function, as given below:

Output will be:

Now we plot this data which will give our required bar plot. Here, density is used to shade the bars.


R Bar Charts 5

Pin It on Pinterest

Share This