gpt4 book ai didi

r - 使用 R 中的州缩写和特征频率创建美国的热图

转载 作者:行者123 更新时间:2023-12-04 05:33:32 24 4
gpt4 key购买 nike

我想创建一张美国 map (可能是热图)来显示各州之间某个特征的频率。我不确定要使用什么包,或者我的数据格式是否正确。我的数据在表中 tf

tf
AB AK AL AN AR AZ CA CO CT DC DE EN FL GA HI IA ID IL IN KS
1 21 31 1 12 56 316 53 31 16 7 1 335 63 11 42 29 73 40 2

在大多数情况下,我的缩写是美国(除了少数加拿大实例)。在 map 上以图形方式显示它的最佳建议方法是什么?

现在如何获得每种颜色小于 50 的粒度?

enter image description here

最佳答案

两个包:
map ,ggplot2。
有一个很好的例子:
?map_data()

只是开始:

tf= structure(list(state = structure(1:14, .Label = c("AK", "AL", 
"AR", "AZ", "CA", "CO", "CT", "DE", "FL", "GA", "IA", "IL", "IN",
"KS"), class = "factor"), num = c(21L, 31L, 12L, 56L, 316L, 53L,
31L, 7L, 335L, 63L, 42L, 73L, 40L, 2L), region = structure(c(2L,
1L, 4L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 13L, 11L, 12L, 14L), .Label = c("alabama",
"alaska", "arizona", "arkansas", "california", "colorado", "connecticut",
"delaware", "florida", "georgia", "illinois", "indiana", "iowa",
"kansas"), class = "factor")), .Names = c("state", "num", "region"
), class = "data.frame", row.names = c(NA, -14L))

require(maps);require(ggplot2)

states <- map_data("state")
tfmerged <- merge(states, tf, sort = FALSE, by = "region")
tfmerged <- tfmerged[order(tfmerged$order), ]
qplot(long, lat, data = tfmerged, group = group, fill = num,
geom="polygon")

然后填写其余的状态信息。

关于r - 使用 R 中的州缩写和特征频率创建美国的热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8537727/

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