4

I have a map of dispersed parts in the Us. This is in the following question (that contains link to the data):

mapping by ggplot2 geom_polygon goes crazy after merging data

It was answered very well. Then I tried to add the US border line, therefore I added the geom_path to the answered code,but no result, it creates the same map just containing the dispersed areas.

library(ggplot2)
#library(tidyverse)
library(dplyr)
library(maps)
load("./data.rda")

usa <- map_data("usa")
shape_map <- tbl_df(fortify(shape, region="Name"))
colnames(shape_map) <- c("long", "lat", "order", "hole", "piece", "region", "group")


ggplot() +
    geom_path(data = usa, aes(long, lat, group=group))+
    geom_map(data=shape_map, map=shape_map, aes(long, lat, map_id=region)) +
    geom_map(
        data=filter(prop.test, season=="DJF"),
        map=shape_map, aes(fill=prop.mega, map_id=megaregion)
    )

Us borderline is not plotted

I have tried geom_polygon() and geom_maps(). no difference. What's the reason, and how can it be solved?

Thank you so much for your help!

BobbyF
  • 431
  • 1
  • 7
  • 19
  • 1
    hint: `range(shape_map$long)`, `range(shape_map$lat)`, `range(usa$long)`, `range(usa$lat)` – hrbrmstr Jan 27 '18 at 03:11
  • 1
    Thank you very much. A very good hint. I am not very knowledgeable about different projection systems. But this hint made me go and learn more about them. – BobbyF Jan 27 '18 at 05:06

1 Answers1

3

So, the problem was the differences in the projections. The us map was in a UTM system, giving Easting and Northing in meters, but named as long and lat. while the US map was in a lat/lon coordinating system. I transformed the shape just before the fortify line in the code as below:

library(ggplot2)
library(tidyverse)

usa <- map_data("usa", )

shape <- spTransform(shape, CRS("+proj=longlat +datum=WGS84"))
shape_map <- fortify(shape, region="Name")

colnames(shape_map) <- c("long", "lat", "order", "hole", "piece", "region", "group")

prop.test <- proptest.result[which(proptest.result$variable=="Upward N"),]

ggplot() +
  geom_map(
    data=usa, map=usa, aes(long, lat, map_id=region),
    color="#2b2b2b", fill="#00000000"
  ) +
  geom_map(
    data=shape_map, map=shape_map, 
    aes(long, lat, map_id=region)
  ) +
  geom_map(
    data=filter(prop.test, season=="DJF"),
    map=shape_map, aes(fill=prop.mega, map_id=megaregion)
  ) +
  viridis::scale_fill_viridis(direction=-1) +
  coord_map("polyconic") +
  ggthemes::theme_map()

and this is the resulted map:

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
BobbyF
  • 431
  • 1
  • 7
  • 19