如何在r中添加特定(选定)美国州地图的边界?

问题描述 投票:0回答:2

我使用比例创建了美国地图。我想添加状态边界来强调数字(颜色),如IL,NH,MI,NY,ND。

如何使用我的数据集绘制状态边界?

我的map_wc看起来如下(它被缩短),每个州都有x中的数字。

> map_wc
                        id       long      lat order  hole piece                  group            x
1                  alabama  -85.05670 32.01738   142 FALSE     1              Alabama.1 0.0024057739
2                  alabama  -85.07007 31.98070   143 FALSE     1              Alabama.1 0.0024057739
3                  alabama  -85.04619 32.09090   139 FALSE     1              Alabama.1 0.0024057739
4                  alabama  -85.05666 32.06964   140 FALSE     1              Alabama.1 0.0024057739
5                  alabama  -86.18148 30.99523    28 FALSE     1              Alabama.1 0.0024057739


library(ggplot2)
library(fiftystater)
p <- ggplot(map_wc, aes(map_id =id)) + 
  # map points to the fifty_states shape data
  geom_map(aes(fill = x), map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_fill_gradient(low="white", high="red", name="Proportion")+
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") +
  theme(legend.position = "bottom")+
    theme(panel.background = element_rect(fill = 'skyblue')) +    
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
    theme(axis.title.x=element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank(),
          axis.title.y=element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank())

enter image description here

谢谢!

r dictionary plot ggplot2
2个回答
2
投票

我没有按照sebdalgamo的建议添加列,而是更喜欢对主数据集进行子集化并向绘图添加另一层的方法,如下所示:

ggplot(fifty_states, aes(map_id = id)) + 
  geom_map(map = fifty_states) +
  geom_map(map = subset(fifty_states, id %in% c('illinois', 'new york')),
           fill = NA, colour = "pink") +
  scale_color_identity() +
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_fill_gradient(low="white", high="red", name="Proportion")+
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") +
  theme(legend.position = "bottom")+
  theme(panel.background = element_rect(fill = 'skyblue'))

enter image description here

这往往是更好的实践,因为我们可以根据需要更改尽可能多的属性而不必创建新列,而不是对颜色进行硬编码。例如,以下代码更改填充,大纲大小和透明度:

 geom_map(map = subset(fifty_states, id %in% c('illinois', 'new york')),
           fill = "yellow", colour = "pink", size = 2, alpha = 0.2) +

enter image description here


1
投票

虽然我没有你的map_wc数据集,但我可以从fifty_states数据集重新创建你想要的东西。这是一种方法:

首先,创建一个将边框颜色传递到要概述的状态的变量

library(ggplot2)
library(fiftystater)
fifty_states <- fiftystater::fifty_states

fifty_states$border <- ifelse(fifty_states$id %in% c('illinois', 'new york'), 'white', NA)

现在你绘制使用aes(color = border)(或任何你命名的新变量)和scale_color_identity(),这意味着ggplot将分配变量边框的颜色(在这种情况下,'white')

p <- ggplot(fifty_states, aes(map_id = id)) + 
  # map points to the fifty_states shape data
  geom_map(map = fifty_states, aes(color = border)) +
  scale_color_identity() +
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_fill_gradient(low="white", high="red", name="Proportion")+
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") +
  theme(legend.position = "bottom")+
  theme(panel.background = element_rect(fill = 'skyblue')) +    
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())

enter image description here

请注意,Mikey Harper的方法可能更适合映射单一颜色。但是,如果您希望不同的状态具有多种颜色的边框,那么您必须将这些作为变量,例如,

fifty_states$border <- ifelse(fifty_states$id %in% c('illinois', 'new york'), 'white', 'orange')
© www.soinside.com 2019 - 2024. All rights reserved.