Map Layer Issues in ggplot2

83 Views Asked by At

I'm having a few issues with finalizing my map for a report. I think I'm warm on the solutions, but haven't quite figured them out. I would really appreciate any help on solutions so that I can finally move on!

1) The scale bar will NOT populate in the MainMap code and the subsequent Figure1 plot. This is "remedied" in the MainMap code if I comment out the "BCWA_land" map layer. However, when I retain the "BCWA_land" map layer it will eliminate the scale bar and produces this error:

Warning message: Removed 3 rows containing missing values (geom_text). 

And this is the code:

MainMap <- ggplot(QOI) +
  geom_sf(aes(fill = quadID)) +
  scale_fill_manual(values = c("#6b8c42", 
                               "#70b2ae", 
                               "#d65a31")) +
  labs(fill = "Quadrants of Interest", 
  caption = "Figure 1: Map depicting the quadrants in the WSDOT project area as well as other quadrants of interest in the Puget Sound area.")+
  ggtitle("WSDOT Project Area and Quadrants of Interest") +
  scalebar(x.min = -123, x.max = -122.8, y.min = 47, y.max = 47.1, location = "bottomleft",
           transform = TRUE,  dist = 10, dist_unit = "km", st.size = 3, st.bottom = TRUE, st.dist = 0.1) +
  north(data = QOI, location = "topleft", scale = 0.1, symbol = 12, x.min = -123, y.min = 48.3, x.max = -122.7, y.max = 48.4) +
  theme_bw()+
  theme(panel.grid= element_line(color = "gray50"),
        panel.background = element_blank(),
        panel.ontop = TRUE,
        legend.text = element_text(size = 11, margin = margin(l = 3), hjust = 0), 
        legend.position = c(0.95, 0.1),
        legend.justification = c(0.85, 0.1), 
        legend.background = element_rect(colour = "#3c4245", fill = "#f4f4f3"),
        axis.title = element_blank(),
        plot.title = element_text(face = "bold", colour = "#3c4245", hjust = 0.5, margin = margin(b=10, unit = "pt")),
        plot.caption = element_text(face = "italic", colour = "#3c4245", margin = margin(t = 7), hjust = 0, vjust = 0.5)) +
  geom_sf(data = BCWA_land) +  #this is what I've tried to comment out to determine the scale bar problem
  xlim (-123.1, -121.4) + 
  ylim (47.0, 48.45)

MainMap

InsetRect <- data.frame(xmin=-123.2, xmax=-122.1, ymin=47.02, ymax=48.45)

InsetMap <- ggplotGrob( ggplot( quads) +
  geom_sf(aes(fill = "")) +
  scale_fill_manual(values = c("#eefbfb"))+
  geom_sf(data = BCWA_land) + 
  scale_x_continuous(expand = c(0,0), limits = c(-124.5, -122.0)) +
  scale_y_continuous(expand = c(0,0), limits = c(47.0, 49.5)) +   
  geom_rect(data = InsetRect,aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
              color="#3c4245",
              size=1.25,
              fill=NA,
              inherit.aes = FALSE) +
    theme_bw()+
    theme(legend.position = "none",
          panel.grid = element_blank(),
          axis.title = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          plot.margin = margin(0,0,0,0)))
InsetMap

Figure1 <- MainMap  + 
 annotation_custom(grob = InsetMap, xmin = -122.2, xmax = -121.3,
                    ymin = 47.75, ymax = 48.5) 


Figure1

As you can see I'm not getting this issue or error for my north arrow so I'm not really sure what is happening with the scale bar!

  1. This problem is probably a little too OCD, however I REALLY don't want the gridlines to show up on the InsetMap, and was hoping that the InsetMap would overlay on top of the MainMap, without gridlines as I had those parameters set to element_blank() in the InsetMap code.

Here is an image of my plot. If you would like the data for this, please let me know. Because these are shapefiles, the data is unwieldy and not super conducive to SO's character limit for a post...

enter image description here

If anyone has any insight into a solution(s) I would so so appreciate that!! Thanks for your time!

1

There are 1 best solutions below

0
On

The issue was the

 xlim (-123.1, -121.4) + 
  ylim (47.0, 48.45)

call that I made. Instead, I used coord_sf(xlim = c(min, max), ylim = c(min, max)). I thought that this would be helpful to someone who might be in my position later on!

Essentially the difference between setting the limits of to the graph using just the x/y lim calls is that that truncates the data that is available in your dataset, whereas the coord_sf call simply "focuses" your graph on that extent if you will without altering the data you have available in your dataset.