I am having issues getting a north arrow and scale bar on my plot.
I built the plot using ggmap and I am using the ggsn package to get a north arrow/scalebar but I am getting the following error:
"Coordinate system already present. Adding new coordinate system, which will replace the existing one. Error in ggsn::scalebar(x.min = -160, x.max = 135, y.min = 15, y.max = 70, : transform should be logical."
Here is my reproducible code for the plot and the error detailed above:
bbox <- c(left = -160, bottom = 15, right = 135, top = 70)
latitude = c(49.38639, 50.68870, 50.77530, 49.86880, 39.31181, 37.05229)
longitude = c(-121.45063, -120.36646, 50, -97.40836, 76.71748, -119.19536)
site_df = as.data.frame(cbind(latitude,longitude))
site_map = ggmap(get_stamenmap(bbox, maptype = "terrain-background", zoom = 2))+
geom_point(data = site_df, aes(x = longitude, y = latitude),
size = 1.5, color = "orange")+
geom_point(data = site_df, aes(x = longitude, y = latitude),
pch= 21, size = 2, color = "black")+
theme_bw() +
labs(x = "Longitude", y = "Latitude")
site_map +
coord_equal() + # needed for ggsn
guides(alpha=FALSE, size=FALSE) +
ggsn::north(x.min = -160, x.max = 135,
y.min = 15, y.max = 70, scale = 0.2) +
ggsn::scalebar(x.min = -160, x.max = 135,
y.min = 15, y.max = 70,
dist = 5, dd2km = TRUE,
model = "WGS84", height = 0.5,
st.dist = 0.5)
I have also tried
site_map +
geom_sf(data = site_df, aes(x = longitude, y = latitude)) +
annotation_scale(location = "bl", width_hint = 0.125) +
annotation_north_arrow(location = "bl", which_north = "true",
height = unit(1, "cm"), width = unit(1, "cm"),
pad_x = unit(0.3, "cm"), pad_y = unit(1.2, "cm"),
style = north_arrow_orienteering) +
coord_sf(xlim = c(-160, 135), ylim = c(15, 70))
But I get the error message:
"Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Error: stat_sf requires the following missing aesthetics: geometry
Run rlang::last_error()
to see where the error occurred.
In addition: Warning message:
Ignoring unknown aesthetics: x, y "
In addition, I am looking to add a legend that says "Sites" for the orange dot and I have used
site_map + theme(legend.position=c(0.9, 0.1))
for this but it does not display.
I would be grateful for any ideas or resources on this. I have researched several methods but none seem to work for this plot. Thanks!