ggplot2|从0开始绘制直方图

简介: ggplot2|从0开始绘制直方图

本文首发于“生信补给站”公众号 ggplot2|从0开始绘制直方图

继续“一图胜千言”系列,直方图(Histogram)又称柱状图,是由一系列高度不等的纵条纹表示数据分布情况,也可以展示数据的概率分布情况。

本文利用R语言的ggplot2包,从头带您绘制各式各样的直方图。


一 绘制基本直方图

准备数据及R包



library(ggplot2)
set.seed(1234)
df <- data.frame(sex = factor(rep(c("F", "M"),each=200)),weight=round(c(rnorm(200, mean=55, sd=5), rnorm(200, mean=65, sd=5)))  )
head(df)
 sex weight
1   F     49
2   F     56
3   F     60
4   F     43
5   F     57
6   F     58


1.1 基本直方图




ggplot(df, aes(x=weight)) +
geom_histogram(binwidth=1,color="black",fill="white")# 改变 bins 和 颜色


1.2 添加均值线





ggplot(df, aes(x=weight)) +
geom_histogram(binwidth=1,color="black", fill="lightblue",linetype="dashed")+ #设置框线类型,颜色和fill的颜色
geom_vline(aes(xintercept=mean(weight)), color="blue", linetype="dashed", size=1) #添加均值线,设置线型,颜色等


image.png

1.3 添加密度曲线


ggplot(df, aes(x=weight)) + 
geom_histogram(aes(y=..density..), colour="black", fill="white")+ # 需要密度形式 
geom_density(alpha=.2, fill="#FF6666")


image.png

二 分组设置颜色 线型等

2.1 分组更改线型颜色



ggplot(df, aes(x=weight, color=sex)) +
geom_histogram(fill="white", alpha=0.5, position="identity")

其中position可选 “identity”, “stack”, “dodge”. 默认值是 “stack”.

image.png



2.2 分组添加均值线


library(plyr)
mu <- ddply(df, "sex", summarise, grp.mean=mean(weight))
p<-ggplot(df, aes(x=weight, color=sex)) +
  geom_histogram(fill="white", position="dodge")+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),             linetype="dashed")+
  theme(legend.position="top")
p


image.png


自定义颜色


# Use custom color palettes
p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p+scale_color_brewer(palette="Dark2")
# Use grey scale
p + scale_color_grey() + theme_classic() + theme(legend.position="top")

分组更改fill的颜色



ggplot(df, aes(x=weight, fill=sex, color=sex)) +  geom_histogram(binwidth=1,position="identity", alpha=0.5)+ geom_vline(data=mu, aes(xintercept=grp.mean),linetype="dashed")


image.png


三 汇总展示


ggplot(df, aes(x=weight, color=sex, fill=sex))+
geom_histogram(binwidth=1,aes(y=..density..), position="identity", alpha=0.5)+
geom_density(alpha=0.6)+
geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),linetype="dashed")+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
labs(title="Weight histogram plot",x="Weight(kg)", y ="Density")+
theme_classic()

image.png


四 参考资料

ggplot2:数据分析与图形艺术

http://www.sthda.com/english/wiki/ggplot2-essentials


OK,输出基本图形后,根据自己的喜好进行细节的调整即可。

相关文章
|
6月前
|
C++
OpenCV-绘制简易直方图DrawHistImg
OpenCV-绘制简易直方图DrawHistImg
|
3月前
|
数据可视化 Python
使用pygal库绘制直方图、XY线图和饼状图的技术指南
使用pygal库绘制直方图、XY线图和饼状图的技术指南
20 0
|
7月前
|
编解码 前端开发 JavaScript
Matplotlib使用和绘制二维图表
Matplotlib使用和绘制二维图表
63 0
|
机器学习/深度学习 API Python
seaborn画直方图、条形图、盒图、散点图等常用图形
seaborn画直方图、条形图、盒图、散点图等常用图形
204 0
seaborn画直方图、条形图、盒图、散点图等常用图形
|
10月前
|
数据挖掘
ggplot2|从0开始绘制折线图
ggplot2|从0开始绘制折线图
117 0
|
10月前
|
数据挖掘
ggplot2|从0开始绘制箱线图
ggplot2|从0开始绘制箱线图
|
10月前
|
算法
ggplot2|从0开始绘制PCA图
ggplot2|从0开始绘制PCA图
295 0
|
10月前
|
数据可视化 数据挖掘 数据处理
ComplexHeatmap|绘制单个热图-I
ComplexHeatmap|绘制单个热图-I
144 0
|
10月前
|
数据挖掘
ggplot2| 绘制KEGG气泡图
ggplot2| 绘制KEGG气泡图
197 0
|
10月前
|
数据建模
R绘图-ggplot2 (2)
R绘图-ggplot2 (2)