直方图和直条图_绘图专题 | 条形图/直方图傻傻分不清楚
相信很多人都會把條形圖、直方圖、柱狀圖混著叫,難以說出其中區(qū)別。
在ggplot2中,其實只有兩個函數(shù)geom_bar()和 geom_histogram(),分別對應(yīng)了條形圖(也有人喜歡叫柱狀圖...),以及直方圖。
所以,這兩個函數(shù)的區(qū)別在哪?
使用 ggplot2 包中提供的 diamonds 數(shù)據(jù)集作為測試數(shù)據(jù):
https://ggplot2.tidyverse.org/reference/diamonds.htmlA dataset containing the prices and other attributes of almost 54,000 diamonds.
p_load(ggplot2)
data(diamonds)
繪制前10個鉆石的價格分布:TestData= diamonds[1:10,]# 使用前10個數(shù)據(jù)
條形圖(Barplot)
在 ggplot2 中圖層函數(shù)geom_bar()可以繪制條形圖:
https://ggplot2.tidyverse.org/reference/geom_bar.htmlgeom_bar()makes the height of the bar proportional to the number of cases in each group (or if theweightaesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, usegeom_col()instead.
ggplot(TestData, aes(x = price)) + geom_bar()
該圖中橫坐標(biāo)為價格,縱坐標(biāo)為每個價格對應(yīng)的鉆石數(shù),所以最終鉆石數(shù)總計為10!
此時如果我們繪制所有的 diamonds 數(shù)據(jù):nrow(diamonds)# 53940
ggplot(diamonds, aes(x = price)) + geom_bar()
可見橫坐標(biāo)price分布過于密集,因為每個價格都被繪制,因為:geom_bar() uses stat_count() by default: it counts the number of cases at each x position.
查看geom_bar()函數(shù)源碼:
function(mapping=NULL,data=NULL,stat='count',position='stack',
...,width=NULL,binwidth=NULL,na.rm=FALSE,show.legend=NA,
inherit.aes=TRUE)
可知,默認(rèn)stat='count',即geom_bar()默認(rèn)對橫坐標(biāo)的每個點(diǎn)(價格)統(tǒng)計數(shù)目!
但是,如果想將價格分割/區(qū)域化,例如統(tǒng)計每100價格區(qū)間對應(yīng)的鉆石數(shù)目,可以設(shè)定 binwidth 參數(shù):ggplot(diamonds,aes(x=price))+geom_bar(binwidth=100)
正常得出結(jié)果:
但有如下警告:Warning message:
geom_bar() no longer has a binwidth parameter. Please use geom_histogram() instead.
即geom_bar()函數(shù)將不再支持binwidth參數(shù)。雖然當(dāng)前還能使用,但建議使用geom_histogram()繪制這種對數(shù)據(jù)進(jìn)行分割/區(qū)域的圖形!
直方圖(Histogram)
https://ggplot2.tidyverse.org/reference/geom_histogram.htmlgeom_histogram()for continuous data.
Visualise the distribution of a single continuous variable by dividing the x axis into binsand counting the number of observations in each bin.
geom_histogram() is an alias for geom_bar() plus stat_bin():stat_bin(), which bins data in ranges and counts the cases in each range. It differs fromstat_count(), which counts the number of cases at eachxposition (without binning into ranges).stat_bin()requires continuousxdata, whereasstat_count()can be used for both discrete and continuousxdata.
可見,與geom_bar()中默認(rèn)使用的stat_count()對單個點(diǎn)的計數(shù)不同,geom_histogram()默認(rèn)使用stat_bin()將數(shù)據(jù)基于一定的范圍分割,并分別統(tǒng)計每個范圍(bin)內(nèi)的數(shù)目。
查看geom_histogram源碼可知,默認(rèn)使用stat='bin',且默認(rèn)分為30個bins:
ggplot(diamonds, aes(x = price)) + geom_histogram()stat_bin()usingbins=30. Pick better value withbinwidth
同樣,可以將范圍設(shè)置為100:ggplot(diamonds,aes(x=price))+geom_histogram(binwidth=100)
此時,得到的圖形結(jié)果將與前文中g(shù)eom_bar(binwidth=100)的結(jié)果一致!
而如果在 geom_histogram()中設(shè)置stat='count',則繪圖效果等同于geom_bar()
ggplot(diamonds,aes(x=price))+geom_histogram(stat='count')
總結(jié)
以上是生活随笔為你收集整理的直方图和直条图_绘图专题 | 条形图/直方图傻傻分不清楚的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery 同级元素下的子元素_jq
- 下一篇: fft函数图像横坐标是什么_10分钟学会