EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials R Programming Tutorial Bar Charts in R
 

Bar Charts in R

Priya Pedamkar
Article byPriya Pedamkar

Updated May 11, 2023

Bar Charts in R

 

 

Introduction Bar Charts in R

Bar Charts in R are the commonly used chart to create a graphical representation of the dataset. The Bar chart is represented as vertical or horizontal bars where the bar length or height indicates the count or frequency or any other calculated measure of the variable. As a best practice, a vector or a matrix can be used as input to the bar chat creation function in R for plotting bar charts. In addition, there are various labels, and color assignment features are available with the bar plot function, which is used to create the bar charts.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

The Basic syntax to create a Bar chart in R is shown below.

barplot (H, xlab, ylab, main, names.arg, col)

Description of the Parameters are:

H denotes height (vector or matrix). If H is a vector, the values determine the heights of the bars. If it is a matrix with option false corresponds to sub bars, and true denotes to create a horizontal bar.

  • xlab: Label for X-axis
  • ylab: Label for Y-axis
  • main: Heading of the bar chart
  • names. arg: Label to the bars a character vector.
  • col: It gives color to the bars in the chart.

How to create a simple Bar Chart in R?

Here we shall discuss how to create Bar charts using function barplot () in R, which is very easy to implement with vertical and horizontal bars. In the below example, we will see creating charts using vectors.

temp <- c(20, 25, 27, 23, 22, 26, 29)
barplot(temp)

Output:

The bar Plot should look like this:

Bar Chart

The next example comes with initializing some vector of numbers and creating a table () command to count them. The width of the bar can be adjusted using a parameter width () and space by space () in the barplot.

// Vector numbers are created using function c ()
x<- c (1,2,2,2,3,5,5,5,5,4)
cnt <- table(x)
cnt
x
barplot (cnt , space =1.0)

vector of number

Creating a Bar chart using R built-in data set with a Horizontal bar. To do so, make horiz = TRUE or else vertical bars are drawn when horiz= FALSE (default option).

We shall consider a R data set as:

Rural Male Rural Female Urban Male Urban Female

## 50-54       11.7          8.7       15.4          8.4

## 55-59       18.1         11.7       24.3         13.6

## 60-64       26.9         20.3       37.0         19.3

## 65-69       41.0         30.9       54.6         35.1

## 70-74       66.0         54.3       71.1         50.0

Here comes an example to plot the built-in data set of R.

a<- VADeaths [2:5, "Urban Male"] barplot(a)
# Horizontal bar plot
barplot (a, horiz = TRUE)

Output:

Barchart A

Barchart B

Creating a Bar Chart with Labels & Title

The bar chart could look more elegant by adding more parameters to the bar plot.

Assigning titles and labels

Titles here are assigned using main arguments as “ Km per distance”, and x-axis as “km and y-axis as “ count” (labels) and the parameter col is for adding colors to the bar( either in hexadecimal or RGB format). also, care should be taken a number of bars should be equal to the number of colours assigned in the character vector; if not, the colors get repeated, density is for shading lines on the bars. Titles and labels can be modified and added to the bar charts.

The following example plots kilometer per count using different parameters.

km <- c(11,14,14,16,17,19,17,16,17,18)
table (km)
km
barplot(table(km),
main="km per distance",
xlab="km",
ylab="Count",
border="brown",
col="yellow",
density=5)

Barchart km

Assigning and changing colors

x <- VADeaths [2:4, "Rural Male"] barplot (x, col = "orange", border = "blue")

The bar chart for the above code is given here:

colour bar

And each of the bars can be assigned different colors. Here, we will fix some labels.

H <- c (6,11,27,2,44)
D <- c("Jan","feb","Mar","Apr","May")
barplot(H,names.arg=D,xlab="Month",ylab="sale",col="Red",main="Salechart",border="yellow")

When executed, we get the following  Output:

Sales chart

Using various Arguments

B <- c (1, 3, 21, 35, 22, 37, 17)
barplot (B, col="green")
barplot (B, main="BARPLOT", xlab="LETTERS", ylab="VALUES", names.arg=c("A","B","C","D","E","F","G"),
border="yellow", density=c (90, 70, 50, 40, 30, 20, 10))

Barplot arg

Barchart km

Using Matrix

mt <- c (3, 1, 10, 12, 14, 7, 9, 11, 18)
val <- matrix (mt, nrow = 3, ncol = 3)
val
barplot (val, col = c ("pink", "yellow", "violet"))

matrix

Multiple comparisons

In the below example, we have created a matrix for three vectors representing five points, and a comparison between them is done using a bar chart. Here, we are using the legend function to display the legends. Bty argument is meant for legend borders. The data has been plotted as follows.

A <- c (2,3,6,4,9)
B <- c (3,5,3,4,11)
C <- c (5,5,7,7,15)
data<- data.frame(A, B, C)
names(data)<- c("Tom","Harry","Gilf")
barplot(height=as.matrix(data),main="Analysis-1",ylab="Vaccine", beside=TRUE,col=rainbow (5))
legend ("topleft",c("Week1","Week2","Week3","Week4","Week5"),cex=2.0,bty="n",fill=rainbow (5))

week chart

Grouped Bar Plots

Bar charts are created for all the columns. (columns are grouped together). Group chart makes use of matrix as input values.

barplot (VADeaths,col = c("blue", "green", "lightcyan","lavender", "magenta"),
legend = rownames(VADeaths),beside = TRUE)

Bar Charts in R - groupbar

// Now Making beside = FALSE

barplot (VADeaths, col = c("blue","green","light cyan","lavender","magenta"),
legend = rownames(VADeaths), beside = FALSE)

Bar Charts in R - groupbar false

Stacked Bar Plot

Instead of assigning the bars continuously, it is effective to stack them in order.

Example:

counts <- table (VADeaths)
barplot(counts, main="Distribution",
xlab="Rural Female",col=c("darkblue","yellow"), legend = rownames(counts))

Bar Charts in R - distribution bar

Conclusion

Hence, we have discussed the basics of creating bar charts in R. this will help you to understand real-time concepts for quantitative comparison. Bar charts play an essential role in data visualizations. We have seen some real-time scenarios on bar charts for categorical values and monitoring variation of a process for the given data set. New variations of bar charts include plotting using dots. Bar charts help in grouping values at several levels.

Recommended Articles

This has been a guide to Bar Charts in R. Here, we discussed the Basic syntax to create a Bar chart, assigning titles and labels using various Arguments. you may also look at the following articles to learn more –
  1. Gantt chart in Tableau
  2. Binomial Distribution in R
  3. How to Create Pie Chart in R?
  4. Steps to Create a Line Graph in R?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW