- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 bar plot with ggplot2 using stat_summary in R?
There are multiple ways to create a bar plot in R and one such way is using stat_summary of ggplot2 package. In this function, we need to supply a function for the y-axis and to create the bars we must use geom="bar". The main thing is to decide which function should be used for y-axis values.
Example
Consider the below data frame:
> x<-sample(c("Male","Female"),20,replace=TRUE) > y<-rpois(20,5) > df<-data.frame(x,y) > df
Output
x y 1 Female 3 2 Male 3 3 Female 7 4 Male 3 5 Female 8 6 Female 5 7 Male 11 8 Male 6 9 Male 5 10 Female 3 11 Female 2 12 Female 7 13 Male 6 14 Female 5 15 Male 3 16 Male 6 17 Female 9 18 Female 6 19 Female 8 20 Female 3
Loading ggplot2 package and creating bar plot using stat_summary:
Example
> library(ggplot2) > ggplot(df,aes(x,y))+stat_summary(fun="mean",geom="bar")
Output
- Related Articles
- How to create bar plot with log values using ggplot2 in R?
- How to create bar plot with gradient colors using ggplot2 in R?
- How to create transparent bar plot using ggplot2 in R?
- How to create a bar plot using ggplot2 with one bar having black border in R?
- How to create a stacked bar plot with vertical bars in R using ggplot2?
- How to create a bar plot using ggplot2 with percentage on Y-axis in R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to create bar plot using ggplot2 with structure data frame?
- How to create bar plot of means with error bars of standard deviations using ggplot2 in R?
- How to create stacked plot with density using ggplot2 in R?
- How to create a bar plot in R with label of bars on top of the bars using ggplot2?
- How to create a dot plot using ggplot2 in R?
- How to create a horizontal bar graph using ggplot2 in R?
- How to create a plot title with unicode characters using ggplot2 in R?
- How to create bar chart using ggplot2 with chart sub-title in R?

Advertisements