bill999
bill999

Reputation: 2527

How to get map to show only desired portion

Say that I have this code (which is modified from OpenStreetMaps autoplot error in RStudio Server and Shiny Server in R4.0.0):

library(maps)
library(OpenStreetMap)
library(ggplot2)
library(tidyverse)
mp <- openmap(c(33,-95), c(43,-73),4,'stamen-watercolor')
states_map <- map_data("state")
states_map_merc <- as.data.frame(projectMercator(states_map$lat,states_map$long))
states_map_merc$group <- states_map$group
counties_map <- map_data("county")
counties_map_merc <- as.data.frame(projectMercator(counties_map$lat,counties_map$long))
counties_map_merc$group <- counties_map$group
OpenStreetMap::autoplot.OpenStreetMap(mp,expand=FALSE) + 
    geom_polygon(data=states_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.5) + 
    geom_polygon(data=counties_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.1)

How can I only get it to make the colored portion of the graph? In other words, only the portion as defined in the openmap command.

It currently looks like:

enter image description here

Also, what do these warning messages mean? Are they anything to worry about?

Warning messages:
1: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
  Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs
2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
  Discarded datum World Geodetic System 1984 in Proj4 definition

Upvotes: 0

Views: 543

Answers (1)

ahmathelte
ahmathelte

Reputation: 623

I obtained the bbox information from the mp object as follows:

> mp[["bbox"]][["p1"]]
[1] -10575352   5311972
> mp[["bbox"]][["p2"]]
[1] -8126323  3895304 

And then modified the limits of the ggplot as follows:

OpenStreetMap::autoplot.OpenStreetMap(mp,expand=FALSE) + 
  geom_polygon(data=states_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.5) + 
  geom_polygon(data=counties_map_merc, aes(x=x,y=y,group=group), fill="black",colour="white",alpha=0, size=.1)+
  
  scale_x_continuous(expand = c(0,0), limits = c(-10575352 ,-8126323))+
  
  scale_y_continuous(expand = c(0,0), limits = c(3895304, 5311972))

The last two lines define the expansion and limits of axes. More information are here

EDIT:The final plot

Upvotes: 3

Related Questions