我的数据如下,我在代码中称之为shortie:
State Min Max Average
1 AL 7.0 10.8 8.842857
2 AK 6.9 10.2 8.095238
3 AZ 5.4 10.0 6.623810
4 AR 9.5 15.4 12.157143
5 CA 5.8 7.9 6.309524
6 CO 6.5 9.8 7.557143
7 CT 5.0 7.9 5.700000
8 DE 5.2 8.4 6.090476
9 FL 7.0 10.9 8.423810
10 GA 6.0 10.3 7.094737
11 HI 15.3 22.6 18.652381
12 ID 7.8 13.9 9.957143
13 IL 5.4 8.8 6.300000
14 IN 6.3 9.6 7.423810
15 IA 6.1 9.0 6.971429
16 KS 5.9 9.2 6.880952
17 KY 6.9 13.5 8.609524
18 LA 5.6 9.6 7.500000
19 ME 7.1 9.7 8.004762
20 MD 5.6 9.7 6.757143
21 MA 5.5 7.9 5.961905
22 MI 5.4 8.2 6.171429
23 MN 5.3 7.7 6.066667
24 MS 4.8 9.4 6.376190
25 MO 6.2 9.6 7.114286
26 MT 7.1 8.6 7.576190
27 NE 6.3 8.0 6.942857
28 NV 28.4 99.0 52.614286
29 NH 6.5 9.5 7.576190
30 NJ 4.8 7.6 5.676190
31 NM 4.0 8.8 7.042857
32 NY 6.5 8.6 7.095238
33 NC 6.5 8.5 7.233333
34 ND 5.8 7.5 6.623810
35 OH 5.7 9.0 6.547619
36 OK 6.5 10.6 7.323529
37 OR 6.3 8.9 7.176190
38 PA 5.3 7.1 5.771429
39 RI 5.8 8.1 6.885714
40 SC 6.6 15.9 8.638095
41 SD 6.7 11.1 8.128571
42 TN 8.2 15.5 10.838095
43 TX 6.9 10.5 7.928571
44 UT 7.3 11.2 9.290476
45 VT 7.9 10.9 9.071429
46 VA 6.7 11.4 7.900000
47 WA 6.0 9.5 6.695238
48 WV 6.1 8.7 7.133333
49 WI 5.2 7.9 5.995238
50 WY 7.1 10.7 8.723810
我尝试了多种方法在美国 map 上绘制这些数据,我想要单独的图,仅包含最大值、最小值或平均值。
plot_usmap(regions="states", values="minimum")
plot_usmap(shortie, regions="states", values="minimum")
plot_usmap(shortie, regions="states", values="Min")
我要么得到空白 map ,要么只是空白屏幕。我尝试将“最小值”定义为 Shortie[2:2],因为有时我会收到错误,指出在数据中未找到 Min 作为列。
这段代码可以运行,但正如您所看到的,它非常长,并且由于某种原因,它没有对佛罗里达赋值,并且如果我包括阿拉斯加和夏威夷,则不会运行。下面的代码不包括阿拉斯加和夏威夷。另外,我想知道除了梯度之外,还有哪些其他替代方法可以通过 ggplot 在视觉上表示数字的最小/最大/平均值。
map.text("state", regions=c("alabama",
"arizona",
"arkansas",
"california",
"colorado",
"connecticut",
"delaware",
"district of columbia",
"florida",
"georgia",
"idaho",
"illinois",
"indiana",
"iowa",
"kansas",
"kentucky",
"louisiana",
"maine",
"maryland",
"massachusetts:main",
"michigan:north",
"minnesota",
"mississippi",
"missouri",
"montana",
"nebraska",
"nevada",
"new hampshire",
"new jersey",
"new mexico",
"new york:main",
"north carolina:main",
"north dakota",
"ohio",
"oklahoma",
"oregon",
"pennsylvania",
"rhode island",
"south carolina",
"south dakota",
"tennessee",
"texas",
"utah",
"vermont",
"virginia:main",
"washington:main",
"west virginia",
"wisconsin",
"wyoming"), labels=as.character(c(7,
5.4,
9.5,
5.8,
6.5,
5,
5.2,
4,
7,
6,
7.8,
5.4,
6.3,
6.1,
5.9,
6.9,
5.6,
7.1,
5.6,
5.5,
5.4,
5.3,
4.8,
6.2,
7.1,
6.3,
28.4,
6.5,
4.8,
4,
6.5,
6.5,
5.8,
5.7,
6.5,
6.3,
5.3,
5.8,
6.6,
6.7,
8.2,
6.9,
7.3,
7.9,
6.7,
6,
6.1,
5.2,
7.1)))
最佳答案
如果您想使用 plot_usmap
,您需要确保向函数提供 2 列数据:
A data frame containing values to plot on the map. This parameter should be a data frame consisting of two columns, a fips code (2 characters for state, 5 characters for county) and the value that should be associated with that region. The columns of data must be fips or state and the value of the 'values' parameter.
因此,如果您想绘制最小值
,您可以传递shortie[,2:3]
:
state Min
1 AL 7.0
2 AK 6.9
3 AZ 5.4
4 AR 9.5
5 CA 5.8
...
请注意,state
列名称应为小写,以符合 plot_usmap
的预期。
那么以下应该可以工作(设置values =“Min”
):
library(usmap)
library(ggplot2)
plot_usmap(data = shortie[,2:3],
regions="states",
values="Min") +
theme(legend.position = "right")
如果要绘制 Max
,请使用 shortie[,c(2,4)]
和 values = "Max"
。
如果您想绘制平均值
,请使用shortie[,c(2,5)]
和values = "Average"
。
关于r - 在美国 map 上绘制数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60022953/