- 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 plot means inside boxplot using ggplot2 in R?
When we create a boxplot, it shows the minimum value, maximum value, first quartile, median, and the third quartile but we might want to plot means as well so that the comparison between factor levels can be made on the basis of means also. To create this type of plot, we first need to find the group-wise means then it can be used with geom_text function of ggplot2.
Example
Consider the CO2 data in base R −
> head(CO2,20) Plant Type Treatment conc uptake 1 Qn1 Quebec nonchilled 95 16.0 2 Qn1 Quebec nonchilled 175 30.4 3 Qn1 Quebec nonchilled 250 34.8 4 Qn1 Quebec nonchilled 350 37.2 5 Qn1 Quebec nonchilled 500 35.3 6 Qn1 Quebec nonchilled 675 39.2 7 Qn1 Quebec nonchilled 1000 39.7 8 Qn2 Quebec nonchilled 95 13.6 9 Qn2 Quebec nonchilled 175 27.3 10 Qn2 Quebec nonchilled 250 37.1 11 Qn2 Quebec nonchilled 350 41.8 12 Qn2 Quebec nonchilled 500 40.6 13 Qn2 Quebec nonchilled 675 41.4 14 Qn2 Quebec nonchilled 1000 44.3 15 Qn3 Quebec nonchilled 95 16.2 16 Qn3 Quebec nonchilled 175 32.4 17 Qn3 Quebec nonchilled 250 40.3 18 Qn3 Quebec nonchilled 350 42.1 19 Qn3 Quebec nonchilled 500 42.9 20 Qn3 Quebec nonchilled 675 43.9 > means <- aggregate(uptake ~ Treatment, CO2, mean) > means Treatment uptake 1 nonchilled 30.64286 2 chilled 23.78333
Creating the boxplot with means for Treatment −
> library(ggplot2) > ggplot(CO2,aes(Treatment,uptake))+geom_boxplot()+ + stat_summary(fun.y=mean,geom="point")+ + geom_text(data=means,aes(label=uptake))
Output
Creating the boxplot with means for Type −
> means <- aggregate(uptake ~ Type, CO2, mean) > ggplot(CO2,aes(Type,uptake))+geom_boxplot()+ + stat_summary(fun.y=mean,geom="point")+ + geom_text(data=means,aes(label=uptake))
Output
- Related Articles
- How to change the order of boxplot by means using ggplot2 in R?
- How to put the plot title inside the plot using ggplot2 in R?
- How to italicize boxplot label in R using ggplot2?
- How to display axes ticks and labels inside the plot using ggplot2 in R?
- How to create boxplot using ggplot2 without whiskers in R?
- How to display mean inside boxplot created by using boxplot function in R?
- How to create bar plot of means with error bars of standard deviations using ggplot2 in R?
- How to highlight text inside a plot created by ggplot2 using a box in R?
- How to create boxplot for list elements using ggplot2 in R?
- How to create a boxplot using ggplot2 with aes_string in R?
- How to create boxplot using ggplot2 without box border in R?
- How to create a scatterplot in R with legend position inside the plot area using ggplot2?
- How to create boxplot with multiple factor levels using ggplot2 in R?
- How to write plot description outside plot in facetted plot using ggplot2 in R?
- How to plot multiple time series using ggplot2 in R?

Advertisements