What is annotation_scale() referencing from coords_sf() that is makes the scale inaccurate?

281 Views Asked by At

I am trying to make a map with with lat and long data. I am able to scale the data plotted on my map using coords_sf() but when I try to add a scale bar using annotation_scale() to the geom_plot(), it is not reorganizing the geometry limits like it is in this example. I have tried to build the map using geom_sf() and coords_sf() to plot the data but had the same outcome (a 2m long scale bar). I have been able to add it with the ggsn::scalebar() function, but am curious about how to make it work using annotation_scale(). Thanks in advance! Please find my code below.

library(ggplot2)
library(maps)
library(mapdata)
library(ggpubr)
library(ggspatial)
library(sf)

region <- map_data("world2Hires")  # the basemap polygon, saved in a object called "region"
region <- subset(region, region %in% c('Canada', 'USA')) # break region into Canada and the USA
region$long = (360 - region$long)*-1    # convert lat/lon

# Save Canada and the USA as different objects
Canada <- subset(region, region == 'Canada') # Canada 
USA <- subset(region, region == 'USA')       # USA 

# Set the coordinates of map

lons = c(-68, -59.5)     # (max longitude, min longitude)
lats = c(43, 48)       # (min latitude, max latitude)


# Create the map using ggplot
# the Longitude/Latitude of each circle comes from the CSV files imported in step 2 

study.area <- ggplot() +
  coord_sf(xlim = lons, ylim = lats) +  
  geom_polygon(data = Canada, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey85") + 
  geom_polygon(data = USA, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey93") +
  xlab("Longitude") + 
  ylab("Latitude") + 
  theme_bw() +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background = element_blank(),
        axis.text = element_text(size = 11),
        axis.title = element_text(size = 12)) +
  theme(plot.margin = unit(c(0.5,0.5,0.2,0.3), "cm")) +
  #geom_point(data = mydata, aes(long, lat, fill=type), colour="black", pch=21) (this is my data plotted as points which I commented out for the sake of the example set)
  labs(shape = "year") +
  geom_jitter() +
  annotation_scale()

study.area
1

There are 1 best solutions below

0
On

Got it! I needed to put the coord_sf() function after the annotate_scale() function.

study.area <- ggplot() + 
  geom_polygon(data = Canada, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey85") + 
  geom_polygon(data = USA, aes(x = long, y = lat, group = group), colour = "grey20", fill = "grey93") +
  xlab("Longitude") + 
  ylab("Latitude") + 
  theme_bw() +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background = element_blank(),
        axis.text = element_text(size = 11),
        axis.title = element_text(size = 12)) +
  theme(plot.margin = unit(c(0.5,0.5,0.2,0.3), "cm")) +
  #geom_point(data = mydata, aes(long, lat, fill=type), colour="black", pch=21) (this is my data plotted as points which I commented out for the sake of the example set)
  labs(shape = "year") +
  geom_jitter() + 
  annotation_scale(location = "bl", width_hint = 0.5) + 
  coord_sf(xlim = lons, ylim = lats, crs = 4326)
study.area