- 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 display mean in a histogram using ggplot2 in R?
To display mean in a histogram using ggplot2, we can use geom_vline function where we need to define the x-intercept value as the mean of the column for which we want to create the histogram. Also, we can change the size of the line for mean in the histogram by using size argument inside geom_vline function.
Consider the below data frame −
x<-rnorm(20000) df<-data.frame(x)
Loading ggplot2 package and creating histogram of x −
Example
library(ggplot2) ggplot(df,aes(x))+geom_histogram(bins=20)
Output
Creating histogram of x with mean displayed on the plot −
Example
ggplot(df,aes(x))+geom_histogram(bins=20)+geom_vline(aes(xintercept=mean(x),size=1))
Output
- Related Articles
- How to display the curve on the histogram using ggplot2 in R?
- How to create a transparent histogram using ggplot2 in R?
- How to create a step histogram using ggplot2 in R?
- How to display mean line per group in facetted graph using ggplot2 in R?
- How to display mean for each group in point chart using ggplot2 in R?
- How to define the breaks for a histogram using ggplot2 in R?
- How to create histogram with varying binwidth using ggplot2 in R?
- How to fill histogram bars using ggplot2 in R with different colors?
- How to display fraction in a plot title using ggplot2 in R?
- How to display two equal signs in R using ggplot2?
- How to display legend on top using ggplot2 in R?
- How to create a histogram with Y-axis values as count using ggplot2 in R?
- How to display a line in segment of a plot using ggplot2 in R?
- How to display 0 at Y-axis using ggplot2 in R?
- Change the outline color for histogram bars using ggplot2 in R.

Advertisements