简体   繁体   English

如何使用 R ggplot 叠加两张地图

[英]How to overlay two maps using R ggplot

I am trying to make a plot of the rates of a type of exposure by regions using the following code.我正在尝试使用以下代码绘制按区域划分的暴露类型的比率图。 I don't need anything plotted by state, but instead would like to display the rates in each of the divisions defined below.我不需要州绘制的任何内容,而是希望显示下面定义的每个部门的费率。 The first map is just the regions I want.第一张地图就是我想要的地区。 This seems to work fine.这似乎工作正常。 But when I try to overlay the rates I am getting crazy shapes in the map.但是当我尝试覆盖率时,我在地图中得到了疯狂的形状。 Map works well, but when i overlay the rates it is not working at all.地图效果很好,但是当我叠加费率时,它根本不起作用。 Does anyone have any suggestions to fix this?有没有人有任何建议来解决这个问题?

             library(ggplot2)
             library(ggmap)
             library(maps)
             library(mapdata)
             library(maptools)
   if (require("maps")) {
 states <- map_data("state")}


us_state_map = map_data("state")
colnames(state_shape)

#map each state to a division
us_state_map$division[us_state_map$region %in% c("connecticut", "maine", "massachusetts", "new hampshire", "rhode island", "vermont")] <- "1"
us_state_map$division[us_state_map$region %in% c("district of columbia", "maryland", "new jersey","pennsylvania","west virginia" ,"delaware")] <- "2"
us_state_map$division[us_state_map$region %in% c("alabama","florida","georgia","arkansas","louisiana", "mississippi")] <- "3"
us_state_map$division[us_state_map$region %in% c("oklahoma","texas")] <- "4"
us_state_map$division[us_state_map$region %in% c("arizona","california","utah", "new mexico", "nevada")] <- "5"
us_state_map$division[us_state_map$region %in% c("alaska","idaho","oregon","washington","montana","hawaii")] <- "6"
us_state_map$division[us_state_map$region %in% c("illinois","north dakota","minnesota","south dakota","wisconsin")] <- "7"
us_state_map$division[us_state_map$region %in% c("colorado","iowa","kansas","missouri","nebraska", "wyoming" )] <- "8"
us_state_map$division[us_state_map$region %in% c("new york")] <- "9"
us_state_map$division[us_state_map$region %in% c("indiana", "michigan", "ohio")] <- "10"
us_state_map$division[us_state_map$region %in% c("north carolina","south carolina","tennessee", "kentucky", "virginia")] <- "11"

map <- ggplot() 
map = map + geom_polygon(data=us_state_map, aes(x=long, y=lat, group=group, fill=division))
map


map <- ggplot() 
map = map + geom_polygon(data=us_state_map.mod, aes(x=long, y=lat, group=group, fill=division))
map

map2<- map + 
  geom_polygon(data = regions_after, aes(x=long, y=lat, group=group, fill = percent_virtual)) +
  geom_polygon(color = "black", fill = NA) + # get the state border back on top
theme_bw() +scale_fill_continuous(low="blue", high="red")+
  ditch_the_axes


map2


after reading both comments, i think this is an appropriate sample: zz <-"division total percent_virtual long lat group region 1 1836 7.5163399 -70.22171 43.59063 18 maine 1 1836 7.5163399 -71.84318 41.34464 46 rhode island 1 1836 7.5163399 -70.6457 41.94624 21 massachusetts 1 1836 7.5163399 -72.20988 41.2988 6 connecticut 1 1836 7.5163399 -73.36152 45.01157 52 vermont 1 1836 7.5163399 -72.50208 42.9661 31 new hampshire 2 6451 1.7516664 -76.55862 38.42828 19 maryland 2 6451 1.7516664 -79.06245 42.00354 45 pennsylvania 2 6451 1.7516664 -74.72516 41.36755 32 new jersey

在此处输入图片说明

A bit convoluted code.有点复杂的代码。 Here simplified.这里简化了。 Note not all the package library calls are relevant.请注意,并非所有包库调用都是相关的。 Just add the color (black) in the same geom_polygon call.只需在同一个geom_polygon调用中添加颜色(黑色)。 There may be a better way of assigning different division names for your states.可能有更好的方法为您的州分配不同的部门名称。

library(ggplot2)
library(mapdata)
#> Loading required package: maps

us_state_map = map_data("state")

#map each state to a division
us_state_map$division[us_state_map$region %in% c("connecticut", "maine", "massachusetts", "new hampshire", "rhode island", "vermont")] <- "1"
us_state_map$division[us_state_map$region %in% c("district of columbia", "maryland", "new jersey","pennsylvania","west virginia" ,"delaware")] <- "2"
us_state_map$division[us_state_map$region %in% c("alabama","florida","georgia","arkansas","louisiana", "mississippi")] <- "3"
us_state_map$division[us_state_map$region %in% c("oklahoma","texas")] <- "4"
us_state_map$division[us_state_map$region %in% c("arizona","california","utah", "new mexico", "nevada")] <- "5"
us_state_map$division[us_state_map$region %in% c("alaska","idaho","oregon","washington","montana","hawaii")] <- "6"
us_state_map$division[us_state_map$region %in% c("illinois","north dakota","minnesota","south dakota","wisconsin")] <- "7"
us_state_map$division[us_state_map$region %in% c("colorado","iowa","kansas","missouri","nebraska", "wyoming" )] <- "8"
us_state_map$division[us_state_map$region %in% c("new york")] <- "9"
us_state_map$division[us_state_map$region %in% c("indiana", "michigan", "ohio")] <- "10"
us_state_map$division[us_state_map$region %in% c("north carolina","south carolina","tennessee", "kentucky", "virginia")] <- "11"

ggplot() +
geom_polygon(data=us_state_map, aes(x=long, y=lat, group=group, fill=division), color = 'black') 

Created on 2020-03-16 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 3 月 16 日创建

Now you want to visualise another dimension with fill , just overlying?现在你想用fill可视化另一个维度,只是覆盖? This is difficult - we are dealing with two dimensional paper here.这很困难——我们在这里处理的是二维纸。 Consider changing the aesthetic.考虑改变审美。 For example, use color for your divisions, and then you can use fill for your precent values.例如,对您的分区使用颜色,然后您可以对您的precent 值使用填充。 It is important that you use the same data frame with the same region polygons (!!!) for assignment of those values.使用具有相同区域多边形 (!!!) 的相同数据框来分配这些值非常重要。

library(tidyverse)

us_state_map <- us_state_map  %>% mutate(percent_virtual = group)

ggplot() +
geom_polygon(data=us_state_map, aes(x=long, y=lat, group=group, color = division, fill = percent_virtual), size = 1.5 )

Created on 2020-03-16 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 3 月 16 日创建

PS I don't necessarily find the above plot solution very good, it is just to demonstrate the use of different aesthetic for different dimensions. PS我不一定觉得上面的情节解决方案很好,只是为了演示不同维度的不同美学的使用。

Thank you for your help, I figured out that is has to do with how the data was merged.感谢您的帮助,我发现这与数据的合并方式有关。 It needs to be a left_join它需要是一个 left_join

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM