简体   繁体   中英

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. 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)

Now you want to visualise another dimension with fill , just overlying? 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. 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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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