

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create barplot from data frame in R using rows as categories?
If we have small number of rows then we might want to create bar plot for rows instead of using columns as categories. This can be done by using barplot function but we need to convert the data frame to matrix and take the transpose of it. For example, if we have a data frame data_frame with 4 rows and 4 columns, then the barplot with rows as categories can be created as barplot(t(as.matrix(data_frame)),beside=TRUE)
Consider the below data frame −
Example
x1<-c(14,15,21,17,16) x2<-c(24,21,15,18,25) x3<-c(21,17,16,19,20) x4<-c(19,27,24,18,20) df<-data.frame(x1,x2,x3,x4) df
Output
x1 x2 x3 x4 1 14 24 21 19 2 15 21 17 27 3 21 15 16 24 4 17 18 19 18 5 16 25 20 20
Creating the barplot by treating rows as categories −
Example
barplot(t(as.matrix(df)),beside=TRUE)
Output
Let’s have a look at another example −
Example
y1<-sample(51:99,4) y2<-sample(51:99,4) y3<-sample(51:99,4) df_y<-data.frame(y1,y2,y3) df_y
Output
y1 y2 y3 1 80 67 72 2 66 56 60 3 70 69 80 4 73 87 61
Creating the barplot by treating rows as categories −
Example
barplot(t(as.matrix(df_y)),beside=TRUE)
Output
- Related Questions & Answers
- How to create stacked barplot using barplot function in R?
- How to create barplot for some top values in an R data frame?
- How to randomly sample rows from an R data frame using sample_n?
- How to plot rows of a data frame as lines in R?
- How to remove empty rows from an R data frame?
- How to remove multiple rows from an R data frame using dplyr package?
- How to remove last few rows from an R data frame?
- How to create a new data frame for the mean of rows of some columns from an R data frame?
- How to remove rows from data frame in R that contains NaN?
- Remove rows from a data frame that exists in another data frame in R.
- How to create scatterplot using data frame columns in R?
- How to delete rows in an R data frame?
- How to add rows in an R data frame?
- How to create a vector of data frame values by rows in R?
- How to remove rows in an R data frame using row names?
Advertisements