oswaldosantos / ggsn

R package to add north symbols and scale bars to maps created with ggplot or ggmap
http://oswaldosantos.github.io/ggsn/
GNU General Public License v2.0
161 stars 9 forks source link

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

Open kenkellner opened 4 years ago

kenkellner commented 4 years ago

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.

https://github.com/oswaldosantos/ggsn/blob/1e2088450f227653d454d92801de0f80e17dbccd/R/scalebar.R#L236