Closed DesiQuintans closed 5 years ago
With ggmap, it is convenient not to use the argument data. You can avoid the problem with:
my_map +
scalebar(x.min = min(coords$long),
x.max = max(coords$long),
y.min = min(coords$lat),
y.max = max(coords$lat),
dist = 5, dist_unit = "km", model = "WGS84",
transform = TRUE)
You proposed solution (commit 0e97ec2) works for this specific code but creates bugs with others. Try this with the scalebar of your commit:
data(domestic_violence)
mp <- as_Spatial(domestic_violence)
mp@data$id <- 1:nrow(mp@data)
mp_df <- merge(tidy(mp), mp, by = "id")
ggplot(mp_df, aes(long, lat, group = group, fill = Scaled)) +
geom_polygon() +
geom_path(color = "black", size = .05) +
coord_equal() +
scalebar(mp_df, dist = 4, dist_unit = "km",
transform = TRUE, model = "WGS84") +
blank() +
scale_fill_continuous(low = "#fff7ec", high = "#7F0000")
Ah I see. I'm guessing you've tried to automate this process before when it comes to a ggmap
pipeline?
Yes, that's the solution I found. Do you have a better one in mind?
Unfortunately not. It should be possible to inherit data
from the ggplot object by writing scalebar()
as a new kind of geom and inheriting it from aes()
(vignette), turning scalebar()
into a generic with a default method for df/ggplot
and a method for sf
. Perhaps that's a nice place for someone else to jump in, since I haven't extended ggplot2 before.
In
ggsn v0.5.0
, the presence of extra columns will makescalebar()
fail when it is time to pipe it together with aggplot
object:And a blank map (white rectangle) is generated. But when the extra column(s) are removed: