- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 boxplots based on two factor data in R?
To create boxplots based on two factor data, we can create facets for one of the factors, where each facet will contain the boxplots for the second factor.
For example, if we have a data frame called df that contains two factor columns say F1 and F2 and one numerical column say Num then the boxplot based on these two factors can be created by using the below given command −
Following snippet creates a sample data frame −
Gender<-sample(c("Male","Score"),20,replace=TRUE) Class<-sample(c("First","Second","Third"),20,replace=TRUE) Score<-sample(1:100,20) df<-data.frame(Gender,Class,Score) df
The following dataframe is created −
Gender Class Score 1 Score First 26 2 Score Second 97 3 Male Third 15 4 Male Second 6 5 Score First 40 6 Score Third 49 7 Male First 60 8 Score Second 41 9 Male Third 82 10 Male First 7 11 Score Third 85 12 Male Second 48 13 Score Third 35 14 Score First 38 15 Score Third 3 16 Male First 51 17 Score Third 37 18 Male First 9 19 Score Third 5 20 Score First 13
To load ggplot2 package and create boxplots for data in df, add the following code to the above snippet −
If you execute all the above given snippets as a single program, it generates the following Output −
- Related Articles
- How to create bar chart based on two groups in an R data frame?
- How to create table of two factor columns in an R data frame?
- How to add a new column in an R data frame with count based on factor column?
- How to select top rows of an R data frame based on groups of factor column?
- Convert a numeric column to binary factor based on a condition in R data frame
- How to combine two factor vectors to create one in R?
- How to create two lines using ggplot2 based on a categorical column in R?
- How to create the boxplots in base R ordered by means?
- How to create a subset of an R data frame based on multiple columns?
- How to create scatterplot for factor levels in an R data frame?
- How to create blue or red colored boxplots in R using ggplot2?
- How to create an ID column in R based on categories?
- How to create a column with ratio of two columns based on a condition in R?
- How to find the two factor interaction variables in an R data frame?
- How to create a subset for a factor level in an R data frame?