Plots
  • Introduction
  • Ⅰ.load the data and install packages
    • 0.load the data and install packages
  • Ⅱ.Plots
    • 1.Box plots
    • 2.Violin plots
    • 3.Histogram plots
    • 4.Density plots
    • 5.Dot plots
    • 6.Scatter plots
    • 7.Volcano plots
    • 8.Manhattan plots
    • 9.Heatmaps
    • 10.Ballon plots
    • 11.Vennpie plots
    • Learn more
Powered by GitBook
On this page
  • 3.1 Basic histogram plot
  • 3.2 Add mean line on a histogram plot
  • 3.3 Change histogram plot fill colors

Was this helpful?

  1. Ⅱ.Plots

3.Histogram plots

Previous2.Violin plotsNext4.Density plots

Last updated 6 years ago

Was this helpful?

3.1 Basic histogram plot

head(df2)
##   sex weight
## 1   F     49
## 2   F     56
## 3   F     60
## 4   F     43
## 5   F     57
## 6   F     58
ggplot(df2, aes(x=weight)) + geom_histogram(binwidth=1)

3.2 Add mean line on a histogram plot

ggplot(df2, aes(x=weight)) + 
  geom_histogram(binwidth=1, color="black", fill="white") +
  geom_vline(aes(xintercept=mean(weight)),color="black", linetype="dashed", size=0.5)

3.3 Change histogram plot fill colors

#Use the plyr package to calculate the average weight of each group :
mu <- ddply(df2, "sex", summarise, grp.mean=mean(weight))
head(mu)
##   sex grp.mean
## 1   F    54.70
## 2   M    65.36
#draw the plot
ggplot(df2, aes(x=weight, color=sex)) +
  geom_histogram(binwidth=1, fill="white", position="dodge")+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex), linetype="dashed") +
  scale_color_brewer(palette="Paired") + 
  theme_classic()+
  theme(legend.position="top")

Reference:

http://www.sthda.com/english/wiki/ggplot2-histogram-plot-quick-start-guide-r-software-and-data-visualization