R – Bar Charts

21 Apr, 2020
A bar chart is a pictorial representation of data that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. In other words, it is the pictorial representation of dataset. These data sets contain the numerical values of variables that represent the length or height.
R uses the function barplot() to create bar charts. Here, both vertical and Horizontal bars can be drawn.
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.
Approach: In order to create a Bar Chart:
A vector (H <- c(Values...)) is taken which contain numeral values to be used.This vector H is plot using barplot().
A vector (H <- c(Values...)) is taken which contain numeral values to be used.
This vector H is plot using barplot().
Example:
# Create the data for the chartA <- c(17, 32, 8, 53, 1)  # Plot the bar chart barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart")
Output:
Approach: To create a horizontal bar chart:
Take all parameters which are required to make simple bar chart.Now to make it horizontal new parameter is added.barplot(A, horiz=TRUE )
Take all parameters which are required to make simple bar chart.
Now to make it horizontal new parameter is added.barplot(A, horiz=TRUE )
barplot(A, horiz=TRUE )
Example: Creating a horizontal bar chart
# Create the data for the chartA <- c(17, 32, 8, 53, 1)   # Plot the bar chart barplot(A, horiz = TRUE, xlab = "X-axis",        ylab = "Y-axis", main ="Bar-Chart")
Output:
Label, title and colors are some properties in the bar chart which can be added to the bar by adding and passing an argument.Approach:
To add the title in bar chart.barplot( A, main = title_name )X-axis and Y-axis can be labeled in bar chart. To add the label in bar chart.barplot( A, xlab= x_label_name, ylab= y_label_name)To add the color in bar chart.barplot( A, col=color_name)
To add the title in bar chart.barplot( A, main = title_name )
barplot( A, main = title_name )
X-axis and Y-axis can be labeled in bar chart. To add the label in bar chart.barplot( A, xlab= x_label_name, ylab= y_label_name)
barplot( A, xlab= x_label_name, ylab= y_label_name)
To add the color in bar chart.barplot( A, col=color_name)
barplot( A, col=color_name)
Example :
# Create the data for the chartA <- 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:
The bar chart can be represented in two form group of bars and stacked.Approach:
Take a vector value and make it matrix M which to be grouped or stacked. Making of matrix can be done by.M <- matrix(c(values...), nrow = no_of_rows, ncol = no_of_column, byrow = TRUE)To display the bar explicitly we can use the beside parameter.barplot( beside=TRUE )
Take a vector value and make it matrix M which to be grouped or stacked. Making of matrix can be done by.M <- matrix(c(values...), nrow = no_of_rows, ncol = no_of_column, byrow = TRUE)
M <- matrix(c(values...), nrow = no_of_rows, ncol = no_of_column, byrow = TRUE)
To display the bar explicitly we can use the beside parameter.barplot( beside=TRUE )
barplot( beside=TRUE )
Example 1:
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 chartbarplot(Values, main = "Total Revenue", names.arg = months,                           xlab = "Month", ylab = "Revenue",                           col = colors, beside = TRUE)  # Add the legend to the chartlegend("topleft", regions, cex = 0.7, fill = colors)
Output:
Example 2:
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 chartbarplot(Values, main = "Total Revenue", names.arg = months,         xlab = "Month", ylab = "Revenue", col = colors)   # Add the legend to the chartlegend("topleft", regions, cex = 0.7, fill = colors)
Output:
Picked
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change column name of a given DataFrame in R
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Adding elements in a vector in R programming - append() method
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
Convert Factor to Numeric and Numeric to Factor in R Programming