purrr 與散佈圖
由於 ggplot() 不接受清單作為輸入,你可以搭配 purrr,用短短幾行程式就把清單轉成資料框,再畫出 ggplot() 圖形。
在這個練習中你會繼續使用 gh_users 資料。你會用一個 map_*() 函式擷取數個具名元素,並轉換成正確的資料型別。接著建立一個散佈圖,將使用者的追蹤者數量與公開儲存庫數量做比較。
本練習屬於課程
使用 purrr 的函式式程式設計基礎
練習說明
- 對
gh_users使用map(),並使用會建立資料框的map_*()函式,產生包含 4 欄且欄名為"login"、"name"、"followers"、"public_repos"的資料框。 - 將該資料框串接(pipe)到散佈圖,
x軸使用followers,y軸使用public_repos。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a data frame with four columns
map___(___, `[`,
c(___,___,___,___)) %>%
# Plot followers by public_repos
ggplot(.,
aes(x = ___, y = ___)) +
# Create scatter plots
geom____()