用cut中的cut_number()绘制彩色美国州地图

时间:2015-03-28 21:00:19

标签: r ggplot2

我有一个名为" drawdata"的数据框:

GeoName Ranking
1   Alabama 15
2   Alaska  2
3   Arizona 28
4   Arkansas    12
5   California  19
6   Colorado    7
7   Connecticut 42
8   Delaware    37
9   District of Columbia    9
10  Florida 38
11  Georgia 11
12  Hawaii  48
13  Idaho   10
14  Illinois    16
15  Indiana 26
16  Iowa    34
17  Kansas  27
18  Kentucky    20
19  Louisiana   4
20  Maine   51
21  Maryland    30
22  Massachusetts   39
23  Michigan    14
24  Minnesota   23
25  Mississippi 41
26  Missouri    32
27  Montana 25
28  Nebraska    21
29  Nevada  45
30  New Hampshire   47
31  New Jersey  33
32  New Mexico  5
33  New York    44
34  North Carolina  13
35  North Dakota    31
36  Ohio    35
37  Oklahoma    6
38  Oregon  18
39  Pennsylvania    40
40  Rhode Island    49
41  South Carolina  29
42  South Dakota    46
43  Tennessee   43
44  Texas   3
45  Utah    17
46  Vermont 50
47  Virginia    8
48  Washington  24
49  West Virginia   22
50  Wisconsin   36
51  Wyoming 1

我想为每个排名绘制一张不同颜色的美国州地图。我的代码是:

  names(drawdata) = c('region','value')
  drawdata[,1] = tolower(drawdata[,1])
  states = data.frame(state.center, state.abb)
  states_map = map_data("state")
  df = merge(drawdata, states_map, by = "region")
  df$num = 49
  p1 = ggplot(data = df, aes(x = long, y = lat, group = group))
  p1 = p1 + geom_polygon(aes(fill = cut_number(value, num[1])))
  p1 = p1 + geom_path(colour = 'gray', linestyle = 2)
  p1 = p1 + scale_fill_brewer('', palette = 'PuRd')
  p1 = p1 + coord_map()
  p1 = p1 + scale_x_continuous(breaks=NULL) + scale_y_continuous(breaks=NULL)
  p1 = p1 + theme(legend.position="none")
  p1 = p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
  p1

如果要填充的颜色数量很少,这完全有效。但是,当我设置' num = 49'时,它会产生错误:

Error in cut.default(x, breaks(x, "n", n), include.lowest = TRUE, ...) : 
  'breaks' are not unique

当我改变

中的代码时
p1 = p1 + geom_polygon(aes(fill = cut_number(value, num[1])))

p1 = p1 + geom_polygon(aes(fill = cut_number(unique(value), num[1])))

然后它给了我一个不同的错误:

Error: Aesthetics must either be length one, or the same length as the dataProblems:cut_number(unique(value), num[1])

我想要一张地图,地图中每49个州都有不同的颜色,每个都反映了他们的排名'。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

Brewer调色板故意具有较小的最大值(通常<12),因为人类几乎不可能将细微差异映射到您拥有的离散值。你可以通过&#34; faking&#34;来实现你正在寻找的东西。它与scale_fill_gradient2(注意:我故意将你的传说留在了你的身边):

library(ggplot2)

names(drawdata) <- c('region','value')
drawdata[,1] <-  tolower(drawdata[,1])

states <- data.frame(state.center, state.abb)
states <- states[!(states$state.abb %in% c("AK", "HI")),] # they aren't part of states_map

states_map <- map_data("state")

p1 <- ggplot()
# borders
p1 <- p1 + geom_map(data=states_map, map=states_map,
                    aes(x=long, y=lat, map_id=region),
                    color="white", size=0.15)
# fills
p1 <- p1 + geom_map(data=drawdata, map=states_map,
                    aes(fill=value, map_id=region),
                    color="white", size=0.15)
# labels
p1 <- p1 + geom_text(data=states, 
                     aes(x=x, y=y, label=state.abb, group=NULL), size=2)
# decent projection
p1 <- p1 + coord_map("albers", lat0=39, lat1=45)
p1 <- p1 + scale_fill_gradient2(low="#f7f4f9", mid="#df65b0", high="#67001f")
# better theme
p1 <- p1 + labs(x=NULL, y=NULL)
p1 <- p1 + theme_bw()
p1 <- p1 + theme(panel.grid=element_blank())
p1 <- p1 + theme(panel.border=element_blank())
p1 <- p1 + theme(axis.ticks=element_blank())
p1 <- p1 + theme(axis.text=element_blank())
p1

enter image description here

您可以使用scale_fill_distiller获得更好的结果,这在幕后很多,可以让您使用带有连续数据的Color Brewer调色板(我认为您没有连续的数据):< / p>

p1 <- p1 + scale_fill_distiller(palette="PuRd")

enter image description here

我强烈建议您继续像原先一样使用cut并且最多有9个休息符合您正在尝试使用的Color Brewer调色板。实际上,人们仍然需要一张桌子才能真正了解排名(绝不假设美国人知道状态形状,位置甚至两个字母的缩写),所以我也只是建议使用实际的表格,如果不代替它,至少有这个等值的全名。

另请注意,您尝试制作地图的方式故意排除阿拉斯加,夏威夷和哥伦比亚特区。你需要使用一个真正的shapefile和我覆盖here之类的东西来让它们很好地显示出来。

答案 1 :(得分:1)

如果您想为每种状态使用不同的颜色,使用渐变,则可以使用scale_fill_gradient。这是一个版本,在渐变的末尾使用绿色和红色,这样每个状态都在那个比例上。

ggplot(data = df, aes(x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = value)) +
  geom_path(colour = 'gray', linestyle = 2) +
  scale_fill_gradient(low = "green", high = "red") +
  coord_map() +
  scale_x_continuous(breaks=NULL) + scale_y_continuous(breaks=NULL) +
  theme(legend.position="none") +
  geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)

enter image description here