paleolimbot / ggspatial

Enhancing spatial visualization in ggplot2
https://paleolimbot.github.io/ggspatial
368 stars 34 forks source link

Overlapping labels and guides() #90

Closed robschick closed 2 years ago

robschick commented 2 years ago

Hi Dewey - I was trying to use guides to minimize label overlap when faceting, but didn't see any change in behavior? Here's a quick reprex:

library(tidyverse)
library(ggplot2)
library(ggspatial)
ndays <- 7
my_days <- seq.Date(from = as.Date("2020-04-01"), length.out = ndays, by = 1)
df <- data.frame(date = rep(my_days, each = ndays * 10),
                 lon = rnorm(n = ndays * 10, mean = -74),
                 lat = rnorm(n = ndays * 10, mean = 35))

esri_ocean <- paste0('https://services.arcgisonline.com/arcgis/rest/services/',
                     'Ocean/World_Ocean_Base/MapServer/tile/${z}/${y}/${x}.jpeg')

df_sf <- sf::st_as_sf(df, coords = c("lon", "lat")) %>% 
  sf::st_set_crs(4326)

ggplot() +
  annotation_map_tile(type = esri_ocean, zoomin = 1, progress = "none") +
  ggspatial::layer_spatial(df_sf, color = "#e6550d", alpha = 0.35)+
  facet_wrap(~ date)+
  theme_bw()

ggplot() +
  annotation_map_tile(type = esri_ocean, zoomin = 1, progress = "none") +
  ggspatial::layer_spatial(df_sf, color = "#e6550d", alpha = 0.35)+
  facet_wrap(~ date)+
  theme_bw()+
  scale_x_continuous(guide = guide_axis(n.dodge = 2))

In either case the labels overlap. I've also experimented with check.overlap, and angle, but the behavior doesn't change. Any suggestions on if I'm doing this wrong?

here's my setup:

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggspatial_1.1.4 forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4     readr_1.3.1     tidyr_1.1.3     tibble_3.1.4    ggplot2_3.3.5   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] nlme_3.1-149       fs_1.5.0           sf_1.0-2           lubridate_1.7.9.2  ash_1.0-15         RColorBrewer_1.1-2 httr_1.4.2         rprojroot_2.0.2   
 [9] tools_4.0.3        backports_1.2.1    utf8_1.2.2         rgdal_1.5-25       R6_2.5.1           KernSmooth_2.23-17 DBI_1.1.1          colorspace_2.0-2  
[17] withr_2.4.2        sp_1.4-5           tidyselect_1.1.1   ggalt_0.4.0        curl_4.3.2         compiler_4.0.3     extrafontdb_1.0    cli_3.0.1         
[25] rvest_0.3.5        xml2_1.3.2         scales_1.1.1       proj4_1.0-10       classInt_0.4-3     proxy_0.4-26       digest_0.6.27      foreign_0.8-80    
[33] rmarkdown_2.6      rio_0.5.16         jpeg_0.1-8.1       pkgconfig_2.0.3    htmltools_0.5.2    extrafont_0.17     dbplyr_1.4.3       fastmap_1.1.0     
[41] maps_3.3.0         rlang_0.4.11       readxl_1.3.1       rstudioapi_0.13    farver_2.1.0       generics_0.1.0     jsonlite_1.7.2     zip_2.0.4         
[49] car_3.0-8          magrittr_2.0.1     Rcpp_1.0.7         munsell_0.5.0      fansi_0.5.0        abind_1.4-5        lifecycle_1.0.0    stringi_1.5.3     
[57] yaml_2.2.1         carData_3.0-4      MASS_7.3-53        plyr_1.8.6         grid_4.0.3         crayon_1.4.1       lattice_0.20-41    haven_2.2.0       
[65] hms_1.1.0          knitr_1.30         pillar_1.6.2       ggpubr_0.4.0       ggsignif_0.6.0     reprex_0.3.0.9001  glue_1.4.2         evaluate_0.14     
[73] data.table_1.14.0  modelr_0.1.6       vctrs_0.3.8        Rttf2pt1_1.3.9     cellranger_1.1.0   gtable_0.3.0       assertthat_0.2.1   xfun_0.25         
[81] openxlsx_4.1.5     broom_0.5.6        e1071_1.7-8        rosm_0.2.5         rstatix_0.6.0      class_7.3-17       units_0.7-2        prettymapr_0.2.2  
[89] ellipsis_0.3.2     here_0.1
paleolimbot commented 2 years ago

Thanks for reporting! This won't work yet because guide_axis() never made it to coord_sf(), if I'm remembering correctly. Maybe use the labels arg as a workaround?

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.1
library(ggspatial)
#> Warning: package 'ggspatial' was built under R version 4.1.1
df <- data.frame(lon = c(45.00001, 45), lat = c(-64.00001, -64))

ggplot(df, aes(lon, lat)) + 
  geom_spatial_point(crs = "OGC:CRS84") +
  scale_x_continuous(labels = function(x) as.character(round(x, 2))) +
  scale_y_continuous(labels = function(x) as.character(round(x, 2))) +
  coord_sf(crs = "EPSG:3857")

Created on 2021-10-05 by the reprex package (v2.0.1)

robschick commented 2 years ago

That explains it! Thanks for the suggested work around