R語言繪制頻率直方圖

頻率直方圖是數(shù)據(jù)統(tǒng)計(jì)中經(jīng)常會用到的圖形展示方式,同時在生物學(xué)分析中可以更好的展示表型性狀的數(shù)據(jù)分布類型;R基礎(chǔ)做圖中的hist函數(shù)對單一數(shù)據(jù)的展示很方便,但是當(dāng)遇到多組數(shù)據(jù)的時候就不如ggplot2繪制來的方便。


1.基礎(chǔ)做圖hist函數(shù)

hist(rnorm(200),col='blue',border='yellow',main='',xlab='')

1.1 多圖展示

par(mfrow=c(2,3))
for (i in 1:6) {hist(rnorm(200),border='yellow',col='blue',main='',xlab='')}

2.ggplot2繪制

  • 構(gòu)造一組正態(tài)分布的數(shù)據(jù)
PH<-data.frame(rnorm(300,75,5))
names(PH)<-c('PH')
#顯示數(shù)據(jù)
head(PH)

##         PH
## 1 72.64837
## 2 67.10888
## 3 89.34927
## 4 75.70969
## 6 82.85354

  • 加載ggplot2作圖包并繪圖
library(ggplot2)
library(gridExtra)
p1<-ggplot(data=PH,aes(PH))+
geom_histogram(color='white',fill='gray60')+ #控制顏色
ylab(label = 'total number') #修改Y軸標(biāo)簽

2.1 修改柱子之間的距離

p2<-ggplot(data=PH,aes(PH))+
geom_histogram(color='white',fill='gray60',binwidth = 3)

2.2 添加擬合曲線

p3<-ggplot(data=PH,aes(PH,..density..))+
geom_histogram(color='white',fill='gray60',binwidth = 3)+
geom_line(stat='density')

2.3 修改線條的粗細(xì)

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)

2.4 繪制密度曲線

p1<-ggplot(data=PH,aes(PH,..density..))+
geom_density(size=1.5)

2.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)

2.6 修改顏色

p4<-ggplot(data=PH,aes(PH,..density..))+
geom_density(size=1.5,linetype=2,colour='red')
grid.arrange(p1,p2,p3,p4)

2.7 多組數(shù)據(jù)展示

  • 構(gòu)造兩組數(shù)據(jù)
df<-data.frame(c(rnorm(200,5000,200),rnorm(200,5000,600)),rep(c('BJ','TJ'),each=200))    
names(df)<-c('salary','city')
  • 結(jié)果展示
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)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 圖形初步 在本章中,我們將討論處理圖形的一般方法。我們首先探討如何創(chuàng)建和保存圖形,然后關(guān)注如何修改那些存在于所有圖...
    jplee閱讀 5,325評論 0 12
  • 首先我是這是我日常逛 twitter 看到的,然后我又是一個搬運(yùn)工, 放最前面的 鏈接來源 :twitter 鏈接...
    熱衷組培的二貨潛閱讀 4,432評論 1 24
  • 搬到新家已經(jīng)一周了,過完了忙碌的四月,今日才得閑早早洗了享受這難得的休閑時光。 我從來就是個念舊的人,老媽說...
    LS玖閱讀 261評論 0 4
  • 三個月, 是我變老了嗎, 看到校園里的學(xué)生, 怎么感覺就那么青澀。 走進(jìn)闊別已久的寢室樓, 一想, 現(xiàn)在已沒有我的...
    三三7閱讀 312評論 0 0
  • 姓名:吳藝琳 公司:無錫宏廣電容器有限公司 組別:第519期六項(xiàng)精進(jìn)利他一組組員 【日精進(jìn)打卡第12天】 【知—學(xué)...
    1530b0c2e3a1閱讀 62評論 0 0

友情鏈接更多精彩內(nèi)容