變更變數型別
在 ggplot2 圖形中呈現資料時,先把各變數轉成正確的型別通常很有幫助。一般來說,像本例的 country 這類類別型變數,在繪圖前應該先轉成因子。你可以使用 as.factor()。在你的資料集中,仍有兩個欄位是「character」型別——請用 mutate() 把它們轉成因子。
本練習屬於課程
在 Tidyverse 中以資料溝通
練習說明
- 將變數
year轉成因子。在轉成因子之前,先用as.numeric()轉成數值:請在as.factor()的呼叫中「內嵌」呼叫as.numeric()。 - 將變數
country轉成因子。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Turn year and country into a factor
ilo_data_corrected <- ilo_data %>%
mutate(year = as.___(as.___(year)),
country = ___)
# See the results
ilo_data_corrected