R語言可視化及作圖5--ggplot2基本要素、幾何對象和數(shù)據(jù)轉(zhuǎn)換函數(shù)匯總


R語言繪圖系列:


ggplot2基本要素

  • “+”和“%+%”
  • 數(shù)據(jù)(data)和映射(mapping):想要可視化的數(shù)據(jù)(data)以及一系列將數(shù)據(jù)中的變量對應(yīng)到圖形屬性的映射(mapping);ggplot2的數(shù)據(jù)(data)必須是一個數(shù)據(jù)框(dataframe)。
  • 幾何對象(geometric):幾何對象(geom)代表你在圖中實際看到的元素,如點、線、多邊形等。
  • 統(tǒng)計變換(statistics):統(tǒng)計變換(stat)是對數(shù)據(jù)進行的某種匯總。例如將數(shù)據(jù)分組計數(shù)以創(chuàng)建直方圖,或?qū)⒁粋€二維的關(guān)系用線性模型進行解釋。統(tǒng)計變換是可選的,但通常非常有用。
  • 標(biāo)度(Scale):標(biāo)度(scale)的作用是將數(shù)據(jù)的取值映射到圖形空間,例如用顏色、大小或形狀來表示不同的取值。展現(xiàn)標(biāo)度的常見做法是繪制圖例和坐標(biāo)軸——它們實際上是從圖形到數(shù)據(jù)的一個映射,使你可以從圖形中讀取原始的數(shù)據(jù)。標(biāo)度包括位置、顏色、大小、形狀、線型。
  • 坐標(biāo)系統(tǒng)(Coordinate):坐標(biāo)系(coord)描述了數(shù)據(jù)是如何映射到圖形所在的平面的,它同時提供了看圖所需的坐標(biāo)軸和網(wǎng)格線。我們通常使用的是笛卡爾坐標(biāo)系,但也可以將其變換為其它類型,如極坐標(biāo)和地圖投影。
  • 圖層(Layer):圖層的作用是生成在圖像上可以被人感知的圖形。一個圖層由4部分組成:數(shù)據(jù)和圖形屬性映射;一種統(tǒng)計變換;一種幾何對象;一種位置調(diào)整方式。
  • 分面(Facet):分面(facet)描述了如何將數(shù)據(jù)分解為各個子集,以及如何對子集作圖并聯(lián)合進行展示。分面也叫作條件作圖或網(wǎng)格作圖。

其中各要素通過“+”以圖層(layer)的方式來粘合構(gòu)圖(可以簡單理解為要素/圖層疊加符號);另外在ggplot2中,數(shù)據(jù)集必須為數(shù)據(jù)框(data.frame)格式,并且可以通過%+%符號調(diào)整已有數(shù)據(jù)集(ggplot2指導(dǎo)文檔中明確寫出“To override the data, you must use %+%”,也就是覆蓋數(shù)據(jù)必須通過%+%)。

完整ggplot2繪圖模板?? :
ggplot(data = <DATA>) +
  <GEOM_FUNCTION>(
    mapping = aes(<MAPPINGS>),
    stat = <STAT>,
    position = <POSITION>
)+ <COORDINATE_FUNCTION> + <FACET_FUNCTION>

幾何對象匯總

幾何對象 功能 屬性
abline 繪制有斜率的直線 color, linetype, size
area 繪制面積圖 color, fill, linetype, size,x,y
bar 繪制條形圖 color, fill, linetype, size, weight, x
bin2d 繪制二維熱圖 color, fill, linetype, size, weight, xmax, ymax, xmin, ymin
boxplot 繪制盒形圖 color, fill, lower, middle, size, upper, weight, x, ymax, ymin
contour 繪制等高線圖 color, linetype, size, weight, x, y
crossbar 繪制盒子圖(不是boxplot) color, fill, linetype, size, x, y, ymax, ymin
density 繪制密度曲線 color, fill, linetype, size, weight, x, y
density2d 二維等高密度曲線 color, linetype, size, weight, x, y
dotplot 繪制點直方圖 color, fill, x, y
errorbar 繪制誤差條 color, linetype, size, weight, x, ymax, ymin
errorbarh 繪制水平誤差條 color, linetype, size, weight, x, ymax, ymin
freqpoly 繪制頻率多邊形 color, linetype, size
hex 繪制六邊形熱圖 color, fill, size, x, y
histogram 繪制直方圖 color, fill, linetype, size, weight, x, y
hline 繪制水平線 color, linetype, size
vline 繪制豎直線 color, linetype, size
jitter 繪制擾動點圖 color, fill, shape, size, x, y
lable 繪制標(biāo)簽 angle, color, hjust, label, size, vjust, x, y
line 繪制線條 color, linetype, size, x, y
linerange 繪制區(qū)間線條 color, linetype, size, x, ymax, ymin
point 繪制散點 color, fill, shape, size, x, y
pointrange 繪制區(qū)間點豎線 color, linetype, fill, shape, size, x, y, ymax, ymin
polygon 繪制多邊形 color, fill, linetype, size, x, y
quantile 添加分位數(shù)回歸線 color, linetype, size, weight, x, y
raster 繪制光柵圖 color, fill, linetype, size, x, y
rect(tile) 繪制矩形(瓦片圖) color, fill, linetype, size, xmax, xmin, ymax, ymin
ribbon 繪制色帶圖 color, fill, linetype, size, x, ymax, ymin
rug 繪制邊際地毯圖 color, linetype, size
segment 添加線段或箭頭 color, linetype, size, x, xend, y, yend
smooth 繪制平滑曲線 color, alpha, fill, linetype, size, weight, x, y
text 添加文本 angle, color, hjust, label, size, vjust, x, y
violin 繪制小提琴圖 weight, color, fill, size, linetype, x, y
path 繪制路線圖 color, linetype, size, x,y

數(shù)據(jù)轉(zhuǎn)換函數(shù)

繪制幾何對象的各個函數(shù)中,數(shù)據(jù)轉(zhuǎn)換函數(shù)均是以參數(shù)“stat”形式出現(xiàn),但是也存在其他數(shù)據(jù)轉(zhuǎn)換函數(shù)。
不同的幾何對象,其默認(rèn)的數(shù)據(jù)轉(zhuǎn)換函數(shù)是有區(qū)別的,尤其是功能迥異的幾個幾何對象,比如geom_point()和geom_bar()。

ggplot2中常見的統(tǒng)計變換及其應(yīng)用的場景總結(jié):

統(tǒng)計變換 功能 應(yīng)用場景
abline 繪制直線 geom_abline()
bin 計算封箱數(shù)據(jù),繪制直方圖或者頻率多邊形 geom_histogram();geom_freqpoly();stat_bin()
bin2d 計算矩形封箱內(nèi)的觀測值個數(shù),繪制二維熱圖 geom_bin2d();stat_bin_2d()
bindot 計算“點直方圖”的封箱數(shù)據(jù) geom_dotplot()
binhex 計算六邊形熱圖的封箱數(shù)據(jù) geom_hex();stat_bin_hex()
boxplot 計算箱線圖的各個元素,繪制箱線圖 geom_bonplot();stat_boxplot()
contour 繪制三維等高線 geom_contour();stat_contour()
density 一維密度估計,繪制密度曲線 geom_density();stat_density()
density2d 二維密度估計,繪制二維密度線圖 geom_density2d();stat_density2d()
function 調(diào)用新函數(shù)進行統(tǒng)計變換 stat_function()
count 對觀測值進行計數(shù) geom_bar();stat_count()
identity 不對數(shù)據(jù)進行統(tǒng)計變換 geom_area();geom_point();geom_errorbar()
qq 計算qq圖指標(biāo),繪制qq圖 geom_qq();stat_qq()
quantile 計算分位數(shù) geom_quantile();stat_quantile()
smooth 添加平滑曲線 geom_smooth();stat_smooth()

ggplot2速查表

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

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

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