gpt4 book ai didi

r - 使用 R 中的美国各州数据绘制(填充颜色) map

转载 作者:行者123 更新时间:2023-12-05 01:03:41 26 4
gpt4 key购买 nike

我有以下数据要绘制在美国的州 map 上:

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

如何将这些绘制为 map 上各州的颜色填充?

我可以绘制 map :
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")
}

但我无法调整它。谢谢你的帮助。

最佳答案

您不需要将逮捕数据与 map 数据合并。您可以将 map 数据单独传递给 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)

关于r - 使用 R 中的美国各州数据绘制(填充颜色) map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24399367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com