在R中创建具有状态缩写和特征频率的美国散热图 [英] Create a heatmap of USA with state abbreviations and characteristic frequency in R

查看:132
本文介绍了在R中创建具有状态缩写和特征频率的美国散热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个美国的地图(也许是一个热图)来显示州之间某一特征的频率。我不知道要使用什么包,或者我的数据是否正确。我的数据在表格中tf

I would like to create a map of the USA (perhaps a heatmap) to show frequency of a certain characteristic among states. I am not sure of what package to use or if my data is in the proper form. My data is in the table 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

在大多数情况下,我的缩写是美国(除了几个加拿大的例子)。在地图上图形显示的最佳建议方法是什么?

For the most part, my abbreviations are US (aside from a few Canadian instances). What is the best suggested approach for graphically displaying this on a map?

现在,我如何获得每种颜色少于50颗粒度?

Now how do I get granularity of less than 50 per color?

推荐答案

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

two packages: maps, ggplot2. There is an excellent example at: ?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")

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

Then fill the rests of the states info.

这篇关于在R中创建具有状态缩写和特征频率的美国散热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆