oswaldosantos/ggsn

scalebar() unit labels are incorrect if sf data object contains a column named "label"

Opened this issue · 0 comments

Using ggsn 0.5.0:

When creating a map with ggplot() and geom_sf() and using scalebar(), if your sf data object has a column named label, the information in that column is used instead of the proper unit labels specified in scalebar(). For example:

library(sf)
library(tidyverse)
library(ggsn)
library(maps)
library(ggplot2)

st <- st_as_sf(map("state", plot=FALSE, fill=TRUE)) %>%
  filter(ID %in% c("connecticut","massachusetts","new hampshire")) %>%
  mutate(label=c("CN","MA","NH"))

ggplot(data=st) +
  geom_sf() +
  scalebar(data=st, dist_unit="km", dist=100, transform=T)

bad

You can work around this by changing the name of the column in the sf object to anything else, e.g.

st <- st %>% rename(.label = label)

ggplot(data=st) +
  geom_sf() +
  scalebar(data=st, dist_unit="km", dist=100, transform=T)

good

Naming the label column scalebar() creates to something other than label and less likely to overlap with sf column names should solve this. I can write a PR if you want.

label = paste0(legend[, "text"], c("", "", "m")))