tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.45k stars 2.02k forks source link

`geom_sf_text()` and `geom_sf_label()` forcing the display of x and y axis labels #5827

Closed elipousson closed 4 months ago

elipousson commented 5 months ago

I found a problem with geom_sf_text() and geom_sf_label() forcing the display of x and y axis labels when labels should be suppressed.

I expected a final call to coord_sf() to over-ride the default behavior for geom_sf_text() and geom_sf_label() (consistent with geom_sf()).

Here is the code to reproduce the bug:

library(ggplot2)

nc <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))

ggplot() +
  geom_sf(
    data = nc
  ) +
  coord_sf(
    label_axes = "----"
  )


ggplot() +
  geom_sf(
    data = nc
  ) +
  geom_sf_text(
    data = nc,
    aes(label = NAME)
  ) +
  coord_sf(
    label_axes = "----"
  )
#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
#> give correct results for longitude/latitude data

Created on 2024-04-04 with reprex v2.1.0

teunbrand commented 5 months ago

The label_axes controls axis labels, not axis titles, so this seems to work as intended. The reason you're seeing the x and y titles is because the text layers converts the geometry columns into regular x and y aesthetics that then, through the default labelling mechanism, lend their names to the axis titles.

elipousson commented 4 months ago

Thanks, @teunbrand. I think I never encountered this because I usually use theme_void() as the base for all my ggplot2 mapping work but recently switched to a different workflow for the theme. An unexpected hiccup in my workflow but definitely not a bug.