频率直方图是数据统计中经常会用到的图形展示方式,同时在生物学分析中可以更好的展示表型性状的数据分布类型;R基础做图中的hist函数对单一数据的展示很方便,但是当遇到多组数据的时候就不如ggplot2绘制来的方便。
***
hist(rnorm(200),col='blue',border='yellow',main='',xlab='')
par(mfrow=c(2,3))
for (i in 1:6) {hist(rnorm(200),border='yellow',col='blue',main='',xlab='')}
PH<-data.frame(rnorm(300,75,5))
names(PH)<-c('PH')
#显示数据
head(PH)
## PH
## 1 72.64837
## 2 67.10888
## 3 89.34927
## 4 75.70969
## 6 82.85354
library(ggplot2)
library(gridExtra)
p1<-ggplot(data=PH,aes(PH))
geom_histogram(color='white',fill='gray60') #控制颜色
ylab(label = 'total number') #修改Y轴标签
p2<-ggplot(data=PH,aes(PH))
geom_histogram(color='white',fill='gray60',binwidth = 3)
p3<-ggplot(data=PH,aes(PH,..density..))
geom_histogram(color='white',fill='gray60',binwidth = 3)
geom_line(stat='density')
p4<-ggplot(data=PH,aes(PH,..density..))
geom_histogram(color='white',fill='gray60',binwidth = 3)
geom_line(stat='density',size=1.5)
grid.arrange(p1,p2,p3,p4)
p1<-ggplot(data=PH,aes(PH,..density..))
geom_density(size=1.5)
p2<-ggplot(data=PH,aes(PH,..density..))
geom_density(size=1.5,linetype=2)
p3<-ggplot(data=PH,aes(PH,..density..))
geom_density(size=1.5,linetype=5)
p4<-ggplot(data=PH,aes(PH,..density..))
geom_density(size=1.5,linetype=2,colour='red')
grid.arrange(p1,p2,p3,p4)
df<-data.frame(c(rnorm(200,5000,200),rnorm(200,5000,600)),rep(c('BJ','TJ'),each=200))
names(df)<-c('salary','city')
library(ggplot2)
p1<-ggplot()
geom_histogram(data=df,aes(salary,..density..,fill=city),color='white')
p2<-ggplot()
geom_histogram(data=df,aes(salary,..density..,fill=city),color='white',alpha=.5)
p3<-ggplot()
geom_density(data=df,aes(salary,..density..,color=city))
p4<-ggplot()
geom_histogram(data=df,aes(salary,..density..,fill=city),color='white') geom_density(data=df,aes(salary,..density..,color=city))
grid.arrange(p1,p2,p3,p4)