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.
barplot(height, xlab, ylab, main, names.arg, col)
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)
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")
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")
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")
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))
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)
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.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.