6
votes

I am attempting to add a scale bar to a ggplot map using ggsn::scalebar(), but despite trying many different combinations of arguments, I always get some error or another. Here is the code for my map:

# load libraries
library(tidyverse)
library(viridis)
library(sp)
library(rgdal)
library(ggsn)

# data
my.data.df <- data.frame(value = c(-1.2269832, -1.0153486, -0.9581069, -1.1534667, -1.0139735, -0.8711997, -0.8618286, -0.8524683),
                         x = c(538127.3, 538129.3, 538125.3, 538127.3, 538129.3,538131.3, 538121.3, 538123.3),
                         y = c(1101055, 1101055, 1101053, 1101053, 1101053, 1101053, 1101051, 1101051))
bb <- data.frame(long = c(538107, 538142), lat = c(1101020.5, 1101058.5))


# make a map
my.map <- ggplot(my.data.df, aes(x=x, y=y, fill=value)) +
  geom_tile() +
  geom_path(data = lines, aes(x=long, y=lat, group=group), inherit.aes = FALSE) +
  scale_fill_viridis("Subsidence (m)",
                     limits = c(-1.5, 0.5)) +
  scale_x_continuous(limits = c(538107, 538142)) +
  scale_y_continuous(limits = c(1101020.5, 1101058.5)) +
  coord_fixed() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        legend.justification=c(1,0),
        legend.position=c(1,0),
        plot.margin=unit(c(0,0,0,0), "cm"))

Take a look at the map here.The problem comes when I try to add a scale bar to the map:

my.map + 
  scalebar(dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft')

This returns the error:

Error in scalebar(dist = 5, dd2km = FALSE, dist_unit = "m", location = "bottomleft") : argument "x.min" is missing, with no default

Adding x.min, x.max, y.min, y.max:

my.map + 
  scalebar(dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft', x.min = 538107, x.max = 538142, y.min = 1101020.5, y.max = 1101058.5)

returns this error:

Error in FUN(X[[i]], ...) : object 'value' not found

Attempting to add the data argument:

my.map + 
  scalebar(data = my.data.df, dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft', x.min = 538107, x.max = 538142, y.min = 1101020.5, y.max = 1101058.5)

Returns the error:

Error: data must be uniquely named but has duplicate columns In addition: Warning messages: 1: In min(data$long) : no non-missing arguments to min; returning Inf 2: In max(data$long) : no non-missing arguments to max; returning -Inf 3: In min(data$lat) : no non-missing arguments to min; returning Inf 4: In max(data$lat) : no non-missing arguments to max; returning -Inf

And, finally, I found help saying that the data argument in scalebar() is actually looking for a bounding box as a data frame, so I attempted to use that:

my.map + 
  scalebar(data = bb, dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft', x.min = 538107, x.max = 538142, y.min = 1101020.5, y.max = 1101058.5)

but got this error:

Error in FUN(X[[i]], ...) : object 'value' not found

I'm wondering if adding scalebar is causing issues with ggplot, because it no longer recognizes "value" as a column in a data frame to use for fill in two of the attempts. However, I'm really not sure. Any insight or solutions would be much appreciated!

1
I actually can't exactly replicate your problem with ggsn 0.4.0. For example, dist_unit = "m" is an unused argument, and when I take it out, I get the following message: argument "model" is missing, with no default. (You also need to call library(ggthemes) in your code because of theme_few(), but this is a minor point.) Can you check the code again?Kim
Try my.map + scalebar(my.data.df, dist = 0.01, height = 0.2, location = 'topleft')Tung
@Kim, thanks for your comment. I meant to take theme.few() out of my example, but must have forgotten. I've edited the question to remove that. I'm working with ggsn 0.4.8, so the errors that you're getting are very different.Heidi Rodenhizer
@Tung, that code gives me this error: Error in scalebar(C17_09_df, dist = 0.01, height = 0.2, location = "topleft") : dd2km should be logical, and when I add dd2km = FALSE, it tells me that the min and max arguments are missing.Heidi Rodenhizer

1 Answers

1
votes

I was having the same issue. By moving the fill mapping from the ggplot call to the geom_tile call it fixed the problem.