3.4 Common problems
談談 R programming 常見的問題~
- Make sure that every ( is matched with a ) and every “ is paired with another “
- 檢查console中的output,若是加號 + 而非 > ,代表RStudio認為輸入的指令(表示式)不完整,RStudio還在等待你輸入。此時可以按下鍵盤上的Esc鍵,強制中斷目前的指令。
- 在使用ggplot包作圖時,連接個別圖層的加號 + 擺放的位置也是常見問題,注意 + 不能擺在每一行的開始,應該放在每一行的結束(句尾)。
- 在console執行
?function_name可以查看此函數的help文件 - Read the error message, Google the error message
3.5 Facet
在3.4節中曾提及,aes()中的引數設定可讓我們為作圖添加額外的自變數,另一個針對類別變數特別實用的作圖技巧則是將plot分隔為數個 facet(即使用data的子集(subset)作圖的subplot)。
facet_wrap() 函數
-
facet_wrap()第一個引數為R語言中的資料結構(稱為formula), 使用時在~後接上分割subplot的自變數名稱 -
facet_wrap()中的第一個引數(用來分割出subplot的自變數),必須是離散型態
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
#以class這個自變數分割出subplot,subplot排列成2 row
facet_grid() 函數:以兩個自變數分割出subplot
- The first argument of
facet_grid()is also a formula. This time the formula should contain two variable names separated by a ~.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
Exercise 3.5.4
What are the advantages to using faceting instead of the color aesthetic? What are the disadvantages? How might the balance change if you had a larger data set?
比較下列兩種引入第三個自變數的方法,程式碼與其output如下:
# facet
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)

# aesthetic mapping
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))

Advantages of encoding
classwith facets instead of color include the ability to encode more distinct categories.Displaying observations from different categories on different scales makes it difficult to directly compare values of observations across categories.
However, it can make it easier to compare the shape of the relationship between the x and y variables across categories.
Disadvantages of encoding the
classvariable with facets instead of the color aesthetic include the difficulty of comparing the values of observations between categories since the observations for each category are on different plots.Since encoding class within color also places all points on the same plot, it visualizes the unconditional relationship between the x and y variables; with facets, the unconditional relationship is no longer visualized since the points are spread across multiple plots.
The benefits encoding a variable through facetting over color become more advantageous as either the number of points or the number of categories increase. In the former, as the number of points increases, there is likely to be more overlap.
由於類別非常多,以aes mapping引入class,用不同顏色資料點代表不同level的作圖,不容易一眼看出不同level之間的區別,例如midsize和minivan這兩種level的資料點不易分辨。facet函數引入自變數分割subplot的方法則可清楚區分出不同level之下的資料點,但因為分割為數個subplot,缺點是不容易看出跨level之間,x軸變數與y軸變數間的關係。
當類別變數的level越多時,或是資料點個數越多時,越適合用facet引入自變數分割subplot的方法(因為當level越多,level間代表的顏色越難一眼分辨;當資料點個數越多,資料點容易重疊在一起,不易分辨)