2

I'm trying to create a map using a base map 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 base map, 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"))

Here's 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) +
  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!

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    i think this is what you're after `scale_x_continuous`, not `scale_x_discrete` https://stackoverflow.com/questions/33302424/format-latitude-and-longitude-axis-labels-in-ggplot – user63230 Jun 08 '20 at 21:34

1 Answers1

0

In order to get your example to work I had to add inherit.aes = FALSE to geom_sf and (as the comment suggested) change the scale to continuous.

The issue with the y-axis was that the label is "20 S" and "19.5 S", and when represented as number should be -20 and -19.5. See the values in the bbox call.

ggmap(SA) +
  geom_sf(data = g, inherit.aes = FALSE) +
  coord_sf(expand = F) +
  scale_x_continuous(breaks = c(33.5, 34, 34.5, 35, 35.5),
                     labels = c("33.5", "34", "34.5", "35", "35.5")) +
  scale_y_continuous(breaks = (c(-20, -19.5, -19, -18.5, -18,-17.5)),
                     labels = c("20", "19.5", "19", "18.5", "18", "17.5"))
nniloc
  • 4,128
  • 2
  • 11
  • 22