# Bar Chart in R: How to Create Bar Plot using barplot()

A bar chart depicts data in rectangular bars with a length of the bar proportional to the value of the variable. To create a bar plot in R, you need to use the barplot(height) function, where height is a vector or matrix. Let’s see how to create a bar chart in R using various use cases.

## Bar Chart in R

To create a bar chart in R, use the barplot() function. The barplot() function creates a bar plot with vertical or horizontal bars. The barplot() function accepts many arguments and, based on that, it will draw the bar chart.

### Syntax

``barplot(height, xlab, ylab, main, names.arg, col)``

### Parameters

height: It is a vector or matrix containing numeric values used in a bar chart.

xlab: It is the label for the x-axis.

ylab: It is the label for the y-axis.

main: It is the title of the bar chart.

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

col: It is used to give colors to the bars in the graph.

There are lots of other parameters, but you will mostly use these.

## Simple barplot Example in R

To create a simple barplot, use the input vector and the name of each bar.

``````data <- c(11, 18, 19, 21, 46)

barplot(data)``````

#### Output In this example, The height is a vector, or we can say our data, so the values determine the heights of the bars in the plot.

In this bar chart, we have not mentioned any x-label, y-label, main title, color, and other properties. But we can define it. Let’s see how to do that.

## Bar Plot Labels, Title, and Colors

To add a title in the bar plot, use the main parameter.

To add the colors in the bar, use the col parameter.

To define an x-axis name, use the xlab and define the y-axis name, use the ylab parameters.

``````data <- c(11, 18, 19, 21, 46)

barplot(data, main = "Enroll Chart",
xlab="x-axis", ylab="y-axis",
col="skyblue")``````

#### Output ## Create a barplot with two vectors in R

In the above example, we have created a chart based on one vector or height, but now we will define two vectors, one for the x-axis and one for the y-axis, and then create a barplot from those values.

``````price <- c(580, 1040, 1980, 2810, 3125)
stocks <- c("Happiest", "TechMahindra", "Mindtree", "LTTS", "TCS")

barplot(price, names.arg= stocks,
main = "Stocks Pile",col="skyblue"
xlab = "Stocks Name", ylab = "Stocks Price")``````

#### Output ## Create a Horizontal barplot from a dataset

R provides many built-in datasets that we can use to create a bar chart. Let’s use the mtcars dataset.

``````counts <- table(mtcars\$gear)
barplot(counts, names.arg=c("3 Gears", "4 Gears", "5 Gears"),
main="Car Distribution",
horiz=TRUE, col="skyblue")``````

#### Output ## Grouped and Stacked Bar Chart in R

The Stacked Bar Chart in R Programming is handy in comparing the data visually. We can create a group bar plot and stack bar plot by using a matrix as input values. More than two variables are represented as a matrix.

### Stacked bar plot

``````counts <- table(mtcars\$vs, mtcars\$gear)
barplot(counts, main = "Car Distribution Channel",
xlab = "Number of Gears", col = c("skyblue", "red"),
legend = rownames(counts))``````

#### Output ### Grouped Bar Plot in R

``````counts <- table(mtcars\$vs, mtcars\$gear)
barplot(counts, main = "Car Distribution Channel",
xlab = "Number of Gears", col = c("skyblue", "red"),
legend = rownames(counts), beside = TRUE)``````

#### Output To add a legend in the barplot, pass the legend argument with the values if you specify legend.text = TRUE, legend values are automatically assigned.

That is it for the bar plot in R tutorial.

### 1 thought on “Bar Chart in R: How to Create Bar Plot using barplot()”

1. Nicely done and written my friend.
I’ve just started writing a blog myself very recently and realized that many bloggers simplyy rehash old ideas
but add very likttle of value. It’s fantastic to read a beneficial article of
some actual value too myself and your other
followers.
It is actually going down on my list off things I need too emulate as
a new blogger. Reaader engagement and material value are king.

Many terrific suggestions; you’ve definitely got on my list of writers
to follow!

Continue the excellent work!
Alll the best,
Pauline