R barplot() Function

The barplot() function in R is used to create a bar chart, useful for visualizing categorical data. The bars can have different colors, and their heights can be based on a vector or matrix of numeric values.

Syntax

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

Parameters

  1. height: A vector or matrix containing numeric values used in a bar chart.
  2. xlab: It is the label for the x-axis.
  3. ylab: It is the label for the y-axis.
  4. main: It is the title of the bar chart.
  5. names.arg: It is a vector of names appearing under each bar.
  6. 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.

Example 1: Basic usage

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

barplot(data)

Output

Bar Plot in R using barplot() Function

Example 2: 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 the y-axis name, and 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

Bar Plot Labels, Title, and Colors

Example 3: Creating a Barplot with two vectors

To create a barplot with two vectors, you can use one vector for the x-axis and one for the y-axis or the matrix with two rows or columns representing the two vectors.

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

Creating a barplot with two vectors

Example 4: Creating a Horizontal barplot from a dataset

To create a horizontal barplot from a dataset in R, you can use the “barplot()” function with the horiz=TRUE argument.

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

Output

Create a Horizontal barplot from a dataset

Example 5: Grouped and Stacked Bar Chart

The Stacked Bar Chart is handy in comparing the data visually. We can create a group and stack barplot 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

Stacked bar plot in R

Grouped BarPlot

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

Grouped bar plot in R

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

1 thought on “R barplot() Function”

  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

    Reply

Leave a Comment