2.1 簡介
qplot的意思是快速作圖(quick plot)。
qplot是一種快捷方式,如果您已習慣於使用基礎plot(),則可以使用它。它可以使用一致的調用模式快速創建許多不同類型的圖。
qplot(x, y, ..., data, facets = NULL, margins = FALSE, geom = "auto", xlim = c(NA, NA), ylim = c(NA, NA), log = "", main = NULL, xlab = NULL, ylab = NULL, asp = NA, stat = NULL, position = NULL)
- x,y:圖中對象的x坐標和y坐標;
- data:可選,用於指定數據框,若進行了指定,那么函數會首先在該數據框內查找變量名,如果沒有指定,將創建一個,從當前環境中提取向量;
- facets:用於圖形分面;
- margins:邏輯值或字符向量。margins是附加的面;
- geom:指定繪圖類型的字符向量。如果指定x和y,默認為“點”;如果指定x,默認為“柱狀圖”;
- xlim,ylim:x和y坐標邊界;
- log:哪些變量要進行對數轉換(“x”、“y”或“xy”);
- main:標題;
- xlab,ylab:x和y坐標軸標簽;
- asp:y/x的長寬比。
2.2 數據集
> head(diamonds) # A tibble: 6 x 10 carat cut color clarity depth table price x y z <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
A dataset containing the prices and other attributes of almost 54,000 diamonds. The variables are as follows:
- price: price in US dollars (\$326–\$18,823)
- carat: weight of the diamond (0.2–5.01)
- cut: quality of the cut (Fair, Good, Very Good, Premium, Ideal)(均勻、良好、非常好、優質、理想)
- color: diamond colour, from D (best) to J (worst)
- clarity: a measurement of how clear the diamond is (I1 (worst), SI2, SI1, VS2, VS1, VVS2, VVS1, IF (best))
- x: length in mm (0–10.74)
- y: width in mm (0–58.9)
- z: depth in mm (0–31.8)
- depth: total depth percentage = z / mean(x, y) = 2 * z / (x + y) (43–79)
- table: width of top of diamond relative to widest point (43–95)
> str(diamonds) Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 53940 obs. of 10 variables: $ carat : num 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ... $ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ... $ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ... $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ... $ depth : num 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ... $ table : num 55 61 65 58 58 57 57 55 61 61 ... $ price : int 326 326 327 334 335 336 336 337 337 338 ... $ x : num 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ... $ y : num 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ... $ z : num 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
可以看到cut、color、clarity為有序分類變量。
抽樣:(方便觀察,加快運算速度)
set.seed(1410) # 讓樣本可重復 dsmall <- diamonds[sample(nrow(diamonds), 100), ]
2.3 基本用法
繪制散點圖。
qplot(carat, price, data = diamonds)
對數變換(對坐標系進行變換,並沒有改變變量的值)。
qplot(carat, price, data = diamonds, log = 'xy')
2.4 顏色、大小、形狀和其他圖形屬性
自動向重量和價格的散點圖中添加顏色和切工的信息。
qplot(carat, price, data = dsmall, colour = color) qplot(carat, price, data = dsmall, shape = cut)
不透明度選擇,在數據量很大時,太多的點重合,影響觀察,使用半透明的顏色可以有效減輕圖形元素重疊的現象,觀察數據集聚趨勢,alpha值可以設定不透明度,取值從0(完全透明)到1(完全不透明),通常用分數來表示,例如1/10或1/20,其分母表示經過多少次重疊之后顏色將變得不透明。
qplot(carat, price, data = diamonds, log = 'xy', alpha = I(1/10))
qplot(carat, price, data = diamonds, colour = color, alpha = I(1/5))
2.5 幾何對象
二維對象:
- geom = "point":繪制散點圖;
- geom = "smooth":擬合一條平滑曲線,並將標准誤展示在圖中,如果不需要展現標准誤可以使用se = FALSE;
- geom = "boxplot":繪制箱線圖,用以概括一系列點的分布情況;
- geom = "jitter":繪制擾動點圖;
- geom = "path"或geom = "line":可以在數據點之間繪制連線,這類圖形的作用是探索時間和其他變量之間的關系,但連線同樣可以用其他的方式將數據點連接起來,線條圖只能創建從左到右的連線,而路徑圖則可以使任意的方向。
一維對象:
- geom = "histogram":繪制直方圖;
- geom = "freqpoly":繪制頻率多邊形;
- geom = "density":繪制密度曲線;
- geom = "bar:對離散變量繪制條形圖。
2.5.1 向圖中添加平滑曲線
使用c()將多個對象傳遞給geom,幾何對象會按照指定的順序進行堆疊。
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))
利用method參數可以選擇不同的平滑器。
- method = "loess"使用的是局部回歸方法,在當n較小是是默認選項,其平滑程度由span控制,取值范圍為0(很不平滑)到1(很平滑)。Loess對於大數據並不合適,因為其內存消耗是$O(n^{2})$。
- 使用method = "gam", formula = y ~ s(x)可以調用mgcv包擬合一個廣義可加模型。對於大數據應使用y ~ s(x, bs = "cs")。
library(mgcv) qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula = y ~ s(x))
- method = "lm"擬合的是線性模型,默認得到一條直線,但可以通過formula = y ~ poly(x, 2)來擬合一個二次多項式。
qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "lm")
- method = "rlm"與"lm"類似,但采用了一種更為穩健的擬合方法,使得結果對異常值不太敏感,這一方法是MASS包的一部分,因此使用時需要先加載MASS包。
2.5.2 箱線圖和擾動點圖
qplot(color, price/carat, data = diamonds, geom = "jitter", alpha = I(1/30)) qplot(color, price/carat, data = diamonds, geom = "boxplot")
每種方法都有它的優勢和不足,箱線圖只用了5個數字進行描述,更具概括性;擾動點圖繪制了全部點,更具全面性。
2.5.3 直方圖和密度曲線圖
直方圖和密度曲線圖可以展現單個變量的分布,相對於箱線圖而言,它們提供了更多的關於單個變量分布的信息,但它們不太容易在不同組之間進行比較。
qplot(carat, data = diamonds, geom = "histogram") qplot(carat, data = diamonds, geom = "density")
對於密度曲線而言,adjust參數控制了曲線的平滑程度(adjust取值越大,曲線越平滑);對於直方圖,binwidth參數通過設置組距來調節平滑度,或者也可以使用breaks對切分位置進行顯示的指定。
在直方圖中,應當嘗試多種組距:組距較大時圖形能夠反應總體特性;組距較小時,則能顯示出更過細節。
要在不同的分組之間進行比較,只需在加上一個圖形映射。
qplot(carat, data = diamonds, geom = "density", colour = color) qplot(carat, data = diamonds, geom = "histogram", fill = color)
密度曲線圖似乎更吸引人,因為很容易閱讀,而且適於在不同的曲線之間進行比較。然而要真正理解密度曲線則比較困難,而且密度曲線有一些隱含的假設,例如曲線應該是無界、連續和平滑的,這些假設不一定適用於真實的數據。
2.5.4 條形圖
用於繪制離散變量,與直方圖類似。條形圖會計算每一個水平下觀測的數量。使用weight可以進行加權。
qplot(color, data = diamonds, geom = "bar") qplot(color, data = diamonds, geom = "bar", weight = carat) + scale_y_continuous("carat")
第一幅圖展現了分組的計數,第二幅圖展現了沒中顏色鑽石的總重量。
2.5.5 時間序列中的線條圖和路徑圖
線條圖和路徑圖用於可視化時間序列數據。線條圖將點從左到右進行連接,而路徑圖則按照點在數據集中的順序進行連接。線條圖的x軸一般是時間,它展現了單個變量隨時間的變化情況;路徑圖則展現了兩個變量隨時間聯動的情況,時間反映在點的連接順序上。
數據集:economics
> head(economics) # A tibble: 6 x 6 date pce pop psavert uempmed unemploy <date> <dbl> <dbl> <dbl> <dbl> <dbl> 1 1967-07-01 507. 198712 12.6 4.5 2944 2 1967-08-01 510. 198911 12.6 4.7 2945 3 1967-09-01 516. 199113 11.9 4.6 2958 4 1967-10-01 512. 199311 12.9 4.9 3143 5 1967-11-01 517. 199498 12.8 4.7 3066 6 1967-12-01 525. 199657 11.8 4.8 3018
該數據集來自美國經濟時間序列數據。
- date:數據收集月份
- psavert:個人儲蓄率
- pce:個人消費支出
- unemploy:失業人數(以千計)
- uempmed:失業持續時間的中位數,以周為單位。
- pop:總人口,以千為單位
繪制時間序列線條圖。(失業率)
qplot(date, unemploy/pop, data = economics, geom = "line")
繪制路徑圖(失業率&失業持續時間)。將時間date映射到colour屬性上更容易看出時間的行進方向。
qplot(unemploy/pop, uempmed, data = economics, geom = c("point", "path"), colour = date)
2.6 分面
分面將數據分隔成若干子集,然后創建一個圖形的矩陣,將一個子集繪制到矩陣的窗格中。
通過row_var ~ col_var的表達式進行指定,如果指向指定一行或者一列,可以使用 . 作為占位符。
qplot(carat, data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3)) qplot(carat, ..density.., data = diamonds, facets = color ~ cut, geom = "histogram", binwidth = 0.1, xlim = c(0, 3))
..density..告訴ggplot2將密度而不是頻數映射到y軸。
2.7 與plot函數的區別
- qplot不是泛型函數,當將不同類型的R對象傳入qplot時,它並不會自動匹配默認的函數調用。ggplot()是一個泛型函數;
- gglpot2中的圖形屬性名稱如colour、shape和size等比基礎繪圖系統中的名稱如col、pch、cex等更直觀,容易記憶;
- 在基礎繪圖系統中,可以通過points()、lines()和text()函數來向已有的圖形中添加更多的元素,而在ggplot2中,你需要在當前的圖形中加入額外的圖層。