How to create side by side barplot in base R?

R ProgrammingServer Side ProgrammingProgramming

If we want to create side by side barplot for two vectors or two columns of same or different data frames then we first need to combine those vectors or columns with the help of cbind function as shown below. After that barplot function will be applied to the combined data and beside argument will be set to TRUE.

Check out the below given example to understand how it works.

Example

To create side by side barplot in base R, use the code given below −

x<-rpois(5,5)
x

If you execute the above given code, it generates the following output −

[1] 3 2 8 1 2

To create side by side barplot in base R, add the following code to the above snippet −

y<-rpois(5,20)
y

If you execute all the above given snippets as a single program, it generates the following output −

[1] 21 21 16 20 17

To create side by side barplot in base R, add the following code to the above snippet −

x<-rpois(5,5)
y<-rpois(5,20)
Combined_xy<-cbind(x,y)
barplot(Combined_xy,beside=TRUE)

Output

If you execute all the above given snippets as a single program, it generates the following output −

raja
Published on 12-Nov-2021 08:05:05
Advertisements