The Wayback Machine - https://web.archive.org/web/20250524105140/https://www.geeksforgeeks.org/r-bar-charts/
Open In App

R - Bar Charts

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Bar charts provide an easy method of representing categorical data in the form of bars. The length or height of each bar represents the value of the category it represents. In R, bar charts are created using the function barplot(), and it can be applied both for vertical and horizontal charts.

Syntax:

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

Parameters:

  • H: This parameter is a vector or matrix containing numeric values which are used in bar chart.
  • xlab: This parameter is the label for x axis in bar chart.
  • ylab: This parameter is the label for y axis in bar chart.
  • main: This parameter is the title of the bar chart.
  • names.arg: This parameter is a vector of names appearing under each bar in bar chart.
  • col: This parameter is used to give colors to the bars in the graph.

Creating a Simple Bar Chart

  • A vector is taken which contains numeral values to be used.
  • This vector H is plot using barplot().
R
# Create the data for the chart
A <- c(17, 32, 8, 53, 1)

# Plot the bar chart
barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart")

Output:

bar_chart_1
R - Bar Charts

A simple bar chart showing information in the form of rectangular bars, with the height or length of the bar indicating the value.

Horizontal Bar Chart

To create a horizontal bar chart, add the horiz = TRUE argument.

R
A <- c(17, 32, 8, 53, 1)

# Plot the bar chart
barplot(A, horiz = TRUE, xlab = "X-axis",
        ylab = "Y-axis", main ="Horizontal Bar Chart"
       )

Output:

Horizontal_bar_chart
Horizontal Bar Chart

Customizing the Bar Chart

Label, title and colors are some properties in the bar chart which can be added to the bar chart

R
A <- c(17, 2, 8, 13, 1, 22)
B <- c("Jan", "feb", "Mar", "Apr", "May", "Jun")

# Plot the bar chart
barplot(A, names.arg = B, xlab ="Month",
        ylab ="Articles", col ="green",
        main ="GeeksforGeeks-Article chart")

Output:

Customize_Bar_Chart
R - GeeksforGeeks-Article chart

Adding Data Labels on Top of Each Bar

You can add the actual values on top of each bar using the text() function.

R
A <- c(17, 2, 8, 13, 1, 22)
B <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")

# Plot the bar chart with text features
barplot(A, names.arg = B, xlab = "Month",
        ylab = "Articles", col = "steelblue",
        main = "GeeksforGeeks - Article Chart",
        cex.main = 1.5, cex.lab = 1.2, cex.axis = 1.1)

# Add data labels on top of each bar
text(
  x = barplot(A, names.arg = B, col = "steelblue", ylim = c(0, max(A) * 1.2)),
  y = A + 1, labels = A, pos = 3, cex = 1.2, col = "black"
)

Output:

GeeksforGeeks - Article Chart-Geeksforgeeks
GeeksforGeeks - Article Chart
  • cex.main, cex.lab, and cex.axis manage the chart title, x-axis label, and y-axis label font size, respectively, to enhance readability.
  • The text() function adds data labels above each bar by placing them slightly above the bar height.

Grouped and Stacked Bar Charts

  • Grouped Bar Chart: Plots bars for different categories side by side.
  • Stacked Bar Chart: Piles bars for different categories on top of one another.

Grouped Bar Chart

R
colors = c("green", "orange", "brown")
months <- c("Mar", "Apr", "May", "Jun", "Jul")
regions <- c("East", "West", "North")

# Create the matrix of the values.
Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11),
                nrow = 3, ncol = 5, byrow = TRUE)

# Create the bar chart
barplot(Values, main = "Total Revenue", names.arg = months,
                        xlab = "Month", ylab = "Revenue",
                        col = colors, beside = TRUE)

# Add the legend to the chart
legend("topleft", regions, cex = 0.7, fill = colors)

Output:

R - Total Revenue -Geeksforgeeks
R - Total Revenue

Stacked Bar Chart

R
colors = c("green", "orange", "brown")
months <- c("Mar", "Apr", "May", "Jun", "Jul")
regions <- c("East", "West", "North")

# Create the matrix of the values.
Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11),
                nrow = 3, ncol = 5, byrow = TRUE)

# Create the bar chart
barplot(Values, main = "Total Revenue", names.arg = months,
        xlab = "Month", ylab = "Revenue", col = colors)

# Add the legend to the chart
legend("topleft", regions, cex = 0.7, fill = colors)

Output:

ing
R - Bar Charts

Related Articles:


Next Article
Practice Tags :

Similar Reads