- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
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 a boxplot using ggplot2 with aes_string in R?
To create a boxplot using ggplot2 with aes_string in R, we can follow the below steps −
- First of all, create a data frame with one string and one numerical column.
- Then, use aes_string function of ggplot2 package to create the boxplot.
Create the data frame
Let's create a data frame as shown below −
X<-sample(LETTERS[1:4],20,replace=TRUE) Count<-sample(1:50,20) df<-data.frame(X,Count) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
X Count 1 A 12 2 B 41 3 A 5 4 A 42 5 C 26 6 D 14 7 D 20 8 B 37 9 A 46 10 D 31 11 A 7 12 D 28 13 B 40 14 D 47 15 D 49 16 D 3 17 C 11 18 D 44 19 B 24 20 B 2
Create the boxplot
Loading ggplot2 package and creating boxplot using aes_string function −
X<-sample(LETTERS[1:4],20,replace=TRUE) Count<-sample(1:50,20) df<-data.frame(X,Count) library(ggplot2) ggplot(data=df,aes_string(x="X",y="Count"))+geom_boxplot()
Output
- Related Articles
- How to create boxplot with multiple factor levels using ggplot2 in R?
- How to create boxplot using ggplot2 without whiskers in R?
- How to create boxplot for list elements using ggplot2 in R?
- How to create boxplot using ggplot2 without box border in R?
- How to create boxplot for categories with grey color palette using ggplot2 in R?
- How to italicize boxplot label in R using ggplot2?
- How to create boxplot for categories with grey color palette in reverse order using ggplot2 in R?
- How to create a boxplot using ggplot2 for single variable without X-axis labels in R?
- How to plot means inside boxplot using ggplot2 in R?
- How to create a bar plot with ggplot2 using stat_summary in R?
- How to create a scatterplot with two legends using ggplot2 in R?
- How to create a scatterplot with dark points using ggplot2 in R?
- How to create stacked plot with density using ggplot2 in R?
- How to create histogram with varying binwidth using ggplot2 in R?
- How to create a point chart with empty points using ggplot2 in R?

Advertisements