Geo 版面配置
在前一個練習中,你看到 plotly 中 geo 版面配置的預設設定。不過,你可以在 layout() 中傳給 geo 的清單裡指定更多引數,來輕鬆自訂。
在這個練習中,你會探索以下幾個實用選項:
- 若要變更陸地顏色,加入引數
showland = TRUE,並設定landcolor。 - 若要讓湖泊與陸地區分開來,加入引數
showlakes = TRUE,並設定lakecolor。 - 若要顯示州/省,設定
showsubunit = TRUE,並設定subunitcolor。 - 若要顯示國家,設定
showcountries = TRUE,並設定countrycolor。
請注意,你必須使用 toRGB() 函式,才能將 R 顏色傳給 geo 版面配置。
plotly 已為你載入。
本練習屬於課程
使用 R 的 plotly 互動式資料視覺化
練習說明
- 針對上一個練習的地圖,透過定義清單
g並將它傳給geo版面配置,來自訂外觀:- 將陸地顏色設為
"gray90"。 - 將湖泊顏色設為
"white"。 - 將州(subunit)邊界顏色設為
"white"。
- 將陸地顏色設為
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Customize the geo layout
g <- list(scope = 'usa',
showland = ___, landcolor = ___,
showlakes = ___, lakecolor = ___,
showsubunit = ___, subunitcolor = ___)
# Apply the geo layout to the map
rallies2018 %>%
plot_geo(locationmode = 'USA-states') %>%
add_markers(
x = ~long, y = ~lat, size = ~no.speakers,
hoverinfo = "text", text = ~paste(city, state, sep = ",")
) %>%
layout(title = "2018 Trump Rallies", geo = ___)