Multiple barplots in R

24 Sep, 2021
Bar plots are a visual representation of the grouped data. It is the most commonly used plot in data analysis activity. In multiple bar plots, we have various bar plots in the form of horizontal and vertical rectangular bars. In these multiple bar plots, the bar represents the relationship between numeric and categorical variables. Let’s learn to create a multiple bar plot with the help of the following examples.
The geom_bar() function is used to create bar charts for categorical data x, and histograms for continuous data y. It is inbuilt in the ggplot2 package, we don’t need to install it separately. 
Syntax: geom_bar( mapping = NULL, data = NULL, stat = “count”, position = “stack”,..)
Parameter:
mapping: mapping can be created by “aes()” i.e. aesthetic mapping in this mapping we provide the column name as an argument to map onto the plot. The default mapping in geom_bar is NULL.
data: data is a data frame that we will be using during the plotting.
stat: stat stands for statistics and its default value is count, i.e. we are creating bars based on the number of counts.
position: position argument specify how the bars will be placed during the visual representation of the figure. The default value of the position is stack.
Here, the position parameter is set to an appropriate value to produce bar plots that are side by side to each other.
Example :
R
# creating multiple bar plots in Rlibrary(ggplot2) # creating a dummy datasetnumber <- c(12,22,11,26,10,20,21,18) gender <- c("Male","Female","Male","Female",            "Female","Male","Female","Male") friend_or_not <- c("Unknown","Friend","Unknown",                   "Friend","Unknown","Friend",                   "Unknown","Friend") # creating data framecircle <- data.frame(number,gender,friend_or_not) # creating plot using the above dataggplot(circle, aes(gender, number, fill = friend_or_not)) +  geom_bar(stat="identity", position = "dodge") +  labs(title="Multiple Bar plots")
Output:
A multiple bar plot using geom_bar()
We can use barplot() function to create a Bar plot in R programming language. Below is the syntax of creating a bar plot. We can use the same bar plot syntax with some modifications to create multiple bar plots.
Syntax: barplot(data,main,xlab,ylab,..)
Parameter:
data: data is where we put our dataset name or table name. We can also supply a vector or matrix as an argument in data.
main: main is used to display the title of the bar chart.
xlab: xlab takes a string as an argument and sets the x-axis label.
ylab: ylab takes a string as an argument and sets the y-axis label.
To put the graphs, side by side, besides parameter of the boxplot() is set to TRUE.
Example:
R
# creating multiple bar plots in R # creating a dummy data framebarplot1=c(10,2,5,4,6,5,8,10,5,9)barplot2=c(9,5,6,4,7,1,2,6,2,6)barplot3=c(4,2,9,4,3,5,7,10,10,3)data <- data.frame(barplot1,barplot2,barplot3) # plotting multiple bar plotsbarplot(as.matrix(data),        main="Multiple Bar Plots",                 # setting y label only        # because x-label will be our        # barplots name        ylab="Count",                 # to plot the bars vertically        beside=TRUE,)
Output:
Multiple Bar plots
 
anikakapoor
Picked
R-Charts
R-ggplot
R-Graphs
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
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)
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
R - if statement