3

I'm trying to create a map using a basemap pulled in using ggmap (get_stamenmap) with some shapefiles plopped on top (geom_sf). I want to change axis labels! Why's it so difficult!

  1. When I only plot my basemap, I get axes labeled with lat/lon and NO characters - i.e. -19

  2. When I add in the shapefiles using geom_sf I get axes labeled with lat/lon AND characters - i.e. 19°S

  3. When I try change axis labels with scale_x_discrete along with sf_coord(expand = F) I can get what I want

  4. When I add scale_y_discrete with label text as I want them I keep getting this error:

Error: Breaks and labels along y direction are different lengths

What is going on here? Are there invisible breaks on the y axis that I'm missing?

This works:

ggmap(SA) +
  geom_sf(data = traj_outSF, alpha = 0.4, inherit.aes = F) +
  coord_sf(expand = FALSE) +
  xlab(expression(paste("Longitude (", degree,"E)"))) +
  ylab(expression(paste("Latitude (", degree,"S)"))) +
  scale_x_discrete(breaks = c(33.5, 34, 34.5, 35, 35.5), labels = c("33.5", "34", "34.5", "35", "35.5"))

and gets me this: correct x axis

As soon as I add in scale_y_discrete I get the error message

ggmap(SA) +
  geom_sf(data = traj_outSF, alpha = 0.4, inherit.aes = F) +
  coord_sf(expand = FALSE) +
  xlab(expression(paste("Longitude (", degree,"E)"))) +
  ylab(expression(paste("Latitude (", degree,"S)"))) +
  scale_x_discrete(breaks = c(33.5, 34, 34.5, 35, 35.5),
                   labels = c("33.5", "34", "34.5", "35", "35.5"))
  scale_y_discrete(breaks = c(20, 19.5, 19, 18.5, 18,17.5),
                   labels = c("20","19.5","19","18.5","18","17.5"))

Heres a reprex:

g = st_sfc(st_point(c(34,-19)))
st_crs(g) <- 4326

SA <- get_stamenmap(bbox = c(33.18, -20.3, 35.8, -17.3), maptype = "toner-lite", zoom = 11)

ggmap(SA) +
  geom_sf(data = g, inherit.aes = F) +
  coord_sf(expand = F) +
  scale_x_discrete(breaks = c(33.5, 34, 34.5, 35, 35.5),
                   labels = c("33.5", "34", "34.5", "35", "35.5")) +
  scale_y_discrete(breaks = c(20, 19.5, 19, 18.5, 18,17.5),
                   labels = c("20","19.5","19","18.5","18","17.5"))

Thanks!

2

I've repeated your example and I wonder why do you need to add additional scale properties? Everything works fine ever without additional scale settings.

As far as I understand you are getting this error message because your y values are below zero (because your object is located in the Southern Hemisphere). Moreover you're trying to control a continuous scale with discrete settings (but this is not necessary). So you need to change the breaks argument in the scale_y_... to negative values.

library(sf)
library(ggmap)

# I've changed the way you creating sf object to remove a warning
g = st_sfc(st_point(c(34,-19)),
           crs = 4326)

SA <- get_stamenmap(bbox = c(33.18, -20.3, 35.8, -17.3),
                    maptype = "toner-lite",
                    zoom = 7) # zoom 7 to download less tiles

# The map itself
ggmap(SA) +
  geom_sf(data = g, inherit.aes = F) +
  coord_sf(expand = F) +
  scale_x_continuous(breaks = c(33.5, 34, 34.5, 35, 35.5)) +
  scale_y_continuous(breaks = seq(-17.3, -20.3, by = -.25))

So the result is below. I've removed an additional labels argument from scales to keep the coordinates format.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.