NBE
NBE

Reputation: 651

R: Centering a map in ggplot2

I would like to center the outline of Jersey in the middle of the bounding box (See attached image below)

enter image description here

This is what the shapefile looks like before using the coord_cartesian(xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543)) enter image description here

Code is as follows:

graph1<-ggplot()+
  geom_polygon(data=middlestates,colour="black",fill="#D3D3D3",aes(x=long,y=lat,group=group))+
  geom_polygon(data=df,colour="black",aes(x=long,y=lat,group=group,fill=ALG))+
  ggtitle("Figure 2.2A: Assessment Results for\nGeneral Aquatic Life Use, Spatial Extent")+
  xlab("")+
  ylab("")+
  coord_cartesian(xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543))+
  ggsn::scalebar(df,location="bottomleft",dist = 50,st.dist=0.02,st.size=3, height=0.01)+
  scale_fill_manual("Aquatic Life Designated\nUse 2014 Assessment",values=c((values=c(colors))))+
  cowplot::background_grid(major= "none",minor = "none") +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y = element_blank(), axis.ticks.y = element_blank(),
        axis.line = element_blank(),
        panel.background = element_blank(),
        legend.position=c(0.8,0.2),
        panel.border = element_rect(fill=NA),
        legend.background = element_blank(),
        legend.text = element_text(size=12),
        legend.title = element_text(colour="black", size=12, face="bold"),
        plot.title=element_text(size=15, face="bold",hjust=0.5))

pdf(file="Figure 2.2A.pdf",width=13,height=12,paper="a4")
north2(graph1, x = 0.10, y = 0.89, scale = 0.1, symbol = 3)
dev.off()

Is there anyway to do this without changing the

coord_cartesian(xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543)) 

line?? I would like to keep the way the map looks. Any help or feedback would be greatly appreciated! Thanks!

Upvotes: 0

Views: 1160

Answers (2)

ASH
ASH

Reputation: 20322

I think it should be something like this.

library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)


states <- map_data("state")
dim(states)
#> [1] 15537     6

head(states)

nj_df <- subset(states, region == "new jersey")
head(nj_df)

counties <- map_data("county")
nj_county <- subset(counties, region == "new jersey")
head(nj_county)


nj_base <- ggplot(data = nj_df, mapping = aes(x = long, y = lat, group = group)) + 
  coord_fixed(1.3) + 
  geom_polygon(color = "black", fill = "gray")
nj_base + theme_nothing()


bj_base + theme_nothing() + 
  geom_polygon(data = nj_county, fill = NA, color = "white") +
  geom_polygon(color = "black", fill = NA)  # get the state border back on top

Upvotes: 0

camille
camille

Reputation: 16842

Here's a mockup of similar shapefiles. I'm using sf, because it's great for quickly filtering or analyzing spatial data, works like dplyr but for shapes, and because it plots easily with the newest version of ggplot2 (might need to use the github version). If your shapefiles are in Spatial* formats, you can use st_as_sf to create an sf object.

To get an sf of states and an sf of New Jersey towns, I used functions from tigris that download shapefiles from the Census. That was just the easiest way I had to get the shapefiles; you use whatever ones you're already working with.

I filtered the mid_sf object, which is an sf of the states in the region, for just New Jersey, then piped it into st_buffer to place a small buffer around it, then st_bbox to get its bounding box.

There are two plots: one without the coordinate limits set, and one with them set based on nj_bbox.

library(tidyverse)
library(sf)

us_sf <- tigris::states(class = "sf", cb = T)

mid_sf <- us_sf %>%
    filter(NAME %in% c("New York", "New Jersey", "Pennsylvania", "Connecticut", "Delaware", "Rhode Island", "Massachusetts", "Maryland", "Virginia", "West Virginia"))

nj_towns_sf <- tigris::county_subdivisions(state = "NJ", class = "sf", cb = T)

# dist is in degrees latitude & longitude
nj_bbox <- mid_sf %>%
    filter(NAME == "New Jersey") %>%
    st_buffer(dist = 0.15) %>%
    st_bbox()
#> Warning in st_buffer.sfc(st_geometry(x), dist, nQuadSegs): st_buffer does
#> not correctly buffer longitude/latitude data

nj_bbox
#>      xmin      ymin      xmax      ymax 
#> -75.70961  38.77852 -73.74398  41.50742

nj_map <- ggplot() +
    geom_sf(data = mid_sf, color = "gray40", fill = "white", size = 0.5) +
    geom_sf(data = nj_towns_sf, color = "gray20", fill = "tomato", alpha = 0.7, size = 0.25)

nj_map

nj_map +
    coord_sf(ndiscr = 0, xlim = c(nj_bbox$xmin, nj_bbox$xmax), ylim = c(nj_bbox$ymin, nj_bbox$ymax))

Created on 2018-05-02 by the reprex package (v0.2.0).

Upvotes: 1

Related Questions