傳統的畫圖函數(如plot、qplot)就好比在一張畫布上畫圖,畫好之後不能修改,而且只能使用一個數據集;而圖層的思想是在多張透明的畫布上繪圖,再把這些畫布按順序疊加在一起形成一個完整的圖形。每一張畫布都可以使用不同數據集,可以單獨對每一個圖層進行修改,需要到哪些圖層就畫哪些圖層。
##1.創建繪圖對象
在用 ggpot()繪製時,需先創建繪圖對象,也就是第一個圖層,它包含兩個參數(數據與圖形屬性映射)。 這兩個參數是繪製圖形的默認參數,如果後面圖層中沒有重新設定新參數,那麼它會自動調用繪圖對象中的參數;如果重新設定了參數則不會使用默認的參數,參數在2.1 中有詳細說明。
ggplot(data, aes(x, y, <other aesthetics>))
歡迎使用Markdown編輯器寫博客
圖層的作用是在圖像上生成可以被人感知的對象。一個圖層有4 個部分組成:
- 數據和圖形屬性映射;
- 一種統計變換;
- 一種幾何對象;
- 一種位置調整方式。
圖層的具體函數如下:
geom_XXX(mapping, data, …, stat, position)
stat_XXX(mapping, data, …, geom, position)
-
mapping(可選):一組圖形屬性映射,通過aes()函數來設定;
-
data(可選):數據框。如果不設定該參數,則會調用繪圖對象中的默認數據集。
-
注意:在繪圖對象ggplot()與圖層中,mapping 與data 的位置是相反的;
-
…:geom 或者 stat 的參數。可以是用圖形屬性作爲參數,這樣該屬性就被設定
爲固定的一個值,而不是被映射給數據集中的一個變量; -
geom 或stat(可選):我們可以修改geom 默認的stat 值,或者stat 默認的geom
值。它們是一組字符串,包含了將要使用的幾何對象或者統計變換的名稱。 -
position(可選):選擇一種調整對象重合的方式。
###2.1 數據與圖層映射
- data 是數據集,數據類型必須是數據框,可選;
- **aes()**函數用來將數據變量映射到圖形中,從而使變量成爲可以被感知的圖形屬性;
- x、y 是映射到x、y 軸上的變量。
對於已經創建好的繪圖對象,可以進行以下操作,以達到添加、修改、刪除圖形映射屬性的效果。例如:
p <- ggplot(mtcars, aes(x=mpg,y=wt))
操作 | 結果 |
---|---|
p + aes(colour = cyl) | 添加colour 屬性映射 |
p + aes(y = disp) | 修改y 軸映射屬性 |
p + aes(y = NULL) | 刪除 y 軸的映射屬性 |
圖層可以添加到 ggplot()與 qplot()創建的圖形對象上。實際上 qplot()繪圖原理是先創建繪圖對象再添加圖層,而相比於 ggplot(),只能針對於一個數據集。
圖層之間用“+”連接,第一個圖層是繪圖對象,單獨運行繪圖對象是不會顯示出圖形的,必須加上圖層纔會顯示。繪圖對象可以儲存到一個變量中,用 summary 函數可以查看繪圖對象中參數的信息。
p <- ggplot(dsmall, aes(carat, price))
p + geom_point()
在p + geom_point()中,geom 是幾何對象,point 是點,表示要繪製散點圖,括號裏的參數都是缺省的,所以它調用了 p 中的默認參數。
p + geom_point() + geom_smooth()
ggplot(dsmall, aes(x = carat, y = price, colour = cut)) + geom_line()
除了可以將變量映射到圖形屬性中之外,也可以將圖形屬性設定爲單一的值(如colour =‘red’)。
p <- ggplot(mtcars, aes(mpg, wt))
p + geom_point(aes(colour = 'red'))
使用分組繪圖,可以將數據按照某個變量分組並以相同的方式渲染。但是從繪製的圖形中只能區分出不同個體,而不能識別出哪個個體。例如:在下列代碼中,按變量cut分組繪製出出五條曲線,你雖然可以區別出五條不同曲線的屬性特徵,但你無法識別出那一條線屬於變量cut 中哪一個值的。
ggplot(dsmall, aes(carat, price, group = cut)) + geom_line()
把變量cut 映射到colour 屬性上,同樣也能達到分組繪圖的目的,並且生成一個圖例,各條線都用不同顏色反映。
ggplot(dsmall, aes(carat, price, colour = cut)) + geom_line()
運用interaction()函數可以組合兩個變量,同時對兩個變量分組繪圖。
ggplot(dsmall, aes(carat, price, group = interaction(cut, color))) + geom_line()
###2.2 幾何對象(geom)
名稱 | 描述 |
---|---|
abline | 線,由斜率與截距決定 |
area | 面積圖(area plot) |
bar | 條形圖,以 x 軸爲底的矩形 |
bin2d | 二維熱圖 |
blank | 空白,什麼也不畫 |
boxplot | 箱線圖 |
contour | 等高線圖 |
crossbar | 帶有水平中心線的盒子圖 |
density | 光滑密度曲線圖 |
density2d | 二維密度等高線圖 |
dotplot | “點直方圖”,用點來表示觀測值的個數 |
errorbar | 誤差棒 |
errorbarh | 水平的誤差棒 |
freqpoly | 頻率多邊形圖 |
hex | 用六邊形表示的二維熱圖 |
histogram | 直方圖 |
hline | 水平線 |
jitter | 給點添加擾動,減輕圖形重疊問題 |
line | 按照 x 座標的大小順序依次連接各個觀測值 |
linerange | 一條代表一個區間的豎直線 |
map | 基準地圖裏的多邊形 |
path | 按數據的原始順序連接各個觀測值 |
point | 點,繪製散點圖 |
pointrange | 用一條中間帶點的豎直線代表一個區間 |
polygon | 多邊形,相當於一個有填充的路徑 |
quantile | 添加分位數迴歸線 |
raster | 高效的矩形瓦片圖 |
rect | 二維的矩形圖 |
ribbon | 色帶圖,連續的 x 值所對應的 y 的範圍 |
rug | 邊際地毯圖 |
segment | 添加線段或箭頭 |
smooth | 添加光滑的條件均值線 |
step | 以階梯形式連接各個觀測值 |
text | 文本註釋 |
tile | 瓦片圖 |
violin | 小提琴圖 |
vline | 豎直圖 |
默認的統計變換(stat)和圖形屬性
|名稱 |默認的統計變換 |圖形屬性
|—|---|
|abline |abline |colour, linetype, size
|area |identity |colour, fill, linetype, size, x, y
|bar |bin |colour, fill, linetype, size, weight, x
|bin2d |bin2d |colour, fill, linetype, size, weight, xmax, xmin, ymax, ymin
|blank |identity|
|boxplot |boxplot|colour, fill, lower, middle,size, upper, weight, x, ymax,ymin
|contour |contour |colour, linetype, size, weight, x, y
|crossbar |identity |colour, fill, linetype, size, x, y, ymax, ymin
|density |density |colour, linetype, size, weight, x, y
|density2d |density2d |colour, linetype, size, weight, x, y
|dotplot |bindot |colour, fill, x, y
|errorbar |identity| colour, linetype, size, width, x, ymax, ymin
|errorbarh |identity| colour, linetype, size, width, x, ymax, ymin
|freqpoly |bin| colour, linetype, size
|hex |binhex |colour, fill, size, x, y
|histogram| bin |colour, fill, linetype, size, weight, x
|hline |hline| colour, linetype, size
|jitter |identity |colour, fill, shape, size, x, y
|line |identity |colour, linetype, size, x, y
|linerange |identity |colour, linetype, size, x, ymax, ymin
|map |identity |colour, fill, linetype, size, x, y, map_id
|path| identity |colour, linetype, size, x, y
|point |identity |colour, fill, shape, size, x, y
|pointrange| identity |colour, fill, linetype, shape, size, x, y, ymax, ymin
|polygon |identity |colour, fill, linetype, size, x, y
|quantile |quantile |colour, linetype, size, weight, x, y
|raster| identity |colour, fill, linetype, size, x, y
|rect |identity |colour, fill, linetype, size, xmax, xmin, ymax, ymin
|ribbon |identity |colour, fill, linetype, size, x, ymax, ymin
|rug |identity |colour, linetype, size
|segment |identity |colour, linetype, size, x, xend, y, yend
|smooth |smooth |alpha, colour, fill, linetype, size, weight, x, y
|step| identity |colour, linetype, size, x, y
|text |identity |angle, colour, hjust, label, size, vjust, x, y
|tile |identity |colour, fill, linetype, size, x, y
|violin| ydensity |weight, colour, fill, size, linetype, x, y
|vline |vline| colour, linetype, size
###2.3 統計變換(stat)
統計變換,對數據進行統計變換,通常以某種方式對數據信息進行彙總。
名稱 | 描述 |
---|---|
bin | 計算封箱(bin)數據 |
bin2d | 計算矩形封箱內的觀測值個數 |
bindot | 計算“點直方圖”的封箱數據 |
binhex | 計算六邊形熱圖的封箱數據 |
boxplot | 計算組成箱線圖的各種元素值 |
contour | 三維數據的等高線 |
density | 一維密度估計 |
density2d | 二維密度估計 |
function | 添加新函數 |
identity | 不對數據進行統計變換 |
計算 qq 圖的相關值 | |
quantile | 計算連續的分位數 |
smooth | 添加光滑曲線 |
spoke | 將角度和半徑轉換成 xend 和yend |
sum | 計算每個單一值的頻數,有助於解決散點圖的圖形重疊問題 |
summary | 對每個 x 所對應的 y 值做統計描述 |
summary2d | 對 2 維矩形封箱設定函數 |
summaryhex | 對 2 維六邊形封箱設定函數 |
unique | 刪除重複值 |
ydensity | 小提琴圖,計算 1 維y 軸方向的核密度函數估計值 |
統計變換會生成新的變量,這些新的變量可以被直接調用。例如,可以用 stat_bin 繪製直方圖,實際上 stat_bin 統計變換會生成以下變量,並會被直接調用:
- count,每一組觀測值數目;
- density,每組觀測值的密度(佔整體百分數/組寬);
- x,組的中心位置。
在以下代碼中,第一個是默認調用了參數 count(可以省略不寫),第二個調用了參數density。調用生成的新變量時必須用“…”包圍起來,以便區分數據集的變量與新生成的變量。查看統計變換的幫助文檔(如?stat_bin),可以知道統計變換新生成的變量以及其他信息。
ggplot(diamonds, aes(carat)) + stat_bin(binwidth = 0.1)
ggplot(diamonds, aes(carat)) + stat_bin(aes(y = ..density..), binwidth = 0.1)
###2.4 位置調整(position)
所謂位置調整,即對圖層元素位置進行微調。以下是ggplot2 中所有可用的位置調整參數。
名稱 | 描述 |
---|---|
dodge | 避免重疊,並排位置 |
fill | 堆疊圖形元素並將高度標準化爲 1 |
identity | 不做任何調整 |
jitter | 給點添加擾動避免重合 |
stack | 將圖形元素堆疊起來 |
在條形圖中可以很好地解釋不同類型的位置調整。以下是三種不同類型的代碼:
ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = 'stack')
ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = 'fill')
ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = 'dodge')
從左到右依次是堆疊(stacking)、填充(filling)、並列(dodging)
對於不同類型的圖,需要合理選擇適合的位置調整。例如:
同一調整(identity adjustment)不適用於條形圖,因爲最頂層圖層會擋住下面的圖層(下圖左);而適用於線型圖,因爲線條不會存在相互遮擋的問題(下圖右)。
代碼分別如下:
ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar(position = 'identity')
ggplot(diamonds, aes(clarity)) + geom_freqpoly(aes(group = cut, colour = cut))
歡迎訪問我的個人站點:http://bgods.cn/