> For the complete documentation index, see [llms.txt](https://xixiaochen-1.gitbook.io/plots/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xixiaochen-1.gitbook.io/plots/.plots/3histogram-plots.md).

# 3.Histogram plots

## 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)
```

## ![](https://3018706235-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LcAcSyy9KcJke9BmqPE%2F-LcAcVJuysC1W3A7ti9F%2F-LcAc_g8qb7zqqMAp5Te%2F3.1.Basic_histogramplot.png?generation=1554973351803075\&alt=media)

## 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)
```

![](https://3018706235-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LcAcSyy9KcJke9BmqPE%2F-LcAcVJuysC1W3A7ti9F%2F-LcAc_gAIXL7xXcMzcVJ%2F3.2.Add_meanline_histogramplot.png?generation=1554973351848917\&alt=media)

## 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")
```

![](https://3018706235-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LcAcSyy9KcJke9BmqPE%2F-LcAcVJuysC1W3A7ti9F%2F-LcAc_gDJa54sQFKk60-%2F3.3.Customized_histogramplot.png?generation=1554973351678429\&alt=media)

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