簡體   English   中英

繪制(填充顏色)地圖與美國陳述R中的數據

[英]Plot (fill color) map with USA states data in R

我有以下數據,我想在美國的州地圖上繪制:

x = data.frame(state.x77[,"Income"])

如何在地圖上將這些顏色填充為狀態?

我可以繪制地圖:

map("state", boundary=TRUE, col=colorRampPalette(c("blue","green","yellow","red"))(50), fill=T)

我可以匹配各州:

match(map("state",plot=F)$names, tolower(rownames(x)))

我試圖從?map_data修改示例:

if (require("maps")) {
states <- map_data("state")
arrests <- USArrests
names(arrests) <- tolower(names(arrests))
arrests$region <- tolower(rownames(USArrests))

choro <- merge(states, arrests, sort = FALSE, by = "region")
choro <- choro[order(choro$order), ]
qplot(long, lat, data = choro, group = group, fill = assault,
  geom = "polygon")
qplot(long, lat, data = choro, group = group, fill = assault / murder,
  geom = "polygon")
}

但我無法調整它。 謝謝你的幫助。

您無需將逮捕數據與地圖數據合並。 您可以將地圖數據單獨傳遞到geom_map圖層。 嘗試

x = data.frame(region=tolower(rownames(state.x77)), 
    income=state.x77[,"Income"], 
    stringsAsFactors=F)

states_map <- map_data("state")
ggplot(x, aes(map_id = region)) + 
    geom_map(aes(fill = income), map = states_map) +
    scale_fill_gradientn(colours=c("blue","green","yellow","red")) + 
    expand_limits(x = states_map$long, y = states_map$lat)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM