hafen / geofacet

R package for geographical faceting with ggplot2
https://hafen.github.io/geofacet/
Other
338 stars 45 forks source link

Secondary yaxis - axis labels and ticks not moved to the closest panel along the margins #389

Closed ht1212 closed 2 months ago

ht1212 commented 2 years ago

I'm using geo_facet and is working great apart from when I'm including a secondary y axis (for my sins) using sec_axis() .

I'm coming across the issue where the axis labels and ticks for the right side yaxis are not moved to the closest panel but instead show for every grid row at the furthest right hand side of the plot. See the example below:

Is there any possibility to have these secondary axis moved to the closest panel as is currently done with the left axis?

Thanks!

`# packages
library(geofacet)
library(ggplot2)

#creating a dummy secondary data line which just + 15 to the values in the df already
state_unemp$dummy_variable_axis <-  state_unemp$rate + 15 

# plotting
ggplot(state_unemp, aes(year, rate)) +
  geom_line() +
  geom_line(state_unemp, mapping=aes(x=year, y=dummy_variable_axis))+
  facet_geo(~ state, grid = "us_state_grid2", move_axes = TRUE) +
  scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
  ylab("Unemployment Rate (%)")+
scale_y_continuous(
  # Add a second axis and specify its features
  sec.axis = sec_axis( trans=~. , name="Dummy variable axis"))`

example_facet_geo_issue

here is my session_info() call:

`
> sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

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

other attached packages:
 [1] reactable_0.3.0      RColorBrewer_1.1-3   emo_0.0.0.9000       ggforce_0.3.4       
 [5] scales_1.2.1         here_1.0.1           lubridate_1.8.0      janitor_2.1.0       
 [9] patchwork_1.1.2      ggrepel_0.9.1        sf_1.0-8             PATHtools_0.0.0.9000
[13] forcats_0.5.2        stringr_1.4.1        purrr_0.3.4          tidyr_1.2.0         
[17] tibble_3.1.8         tidyverse_1.3.2      readr_2.1.2          dplyr_1.0.9         
[21] geofacet_0.2.0       ggplot2_3.3.6       

loaded via a namespace (and not attached):
 [1] fs_1.5.2            bit64_4.0.5         httr_1.4.4          rprojroot_2.0.3    
 [5] tools_4.2.1         backports_1.4.1     utf8_1.2.2          R6_2.5.1           
 [9] KernSmooth_2.23-20  rgeos_0.5-9         DBI_1.1.3           colorspace_2.0-3   
[13] withr_2.5.0         sp_1.5-0            tidyselect_1.1.2    gridExtra_2.3      
[17] bit_4.0.4           curl_4.3.2          compiler_4.2.1      cli_3.3.0          
[21] rvest_1.0.3         xml2_1.3.3          labeling_0.4.2      classInt_0.4-7     
[25] proxy_0.4-27        digest_0.6.29       rmarkdown_2.15      jpeg_0.1-9         
[29] pkgconfig_2.0.3     htmltools_0.5.3     dbplyr_2.2.1        fastmap_1.1.0      
[33] htmlwidgets_1.5.4   rlang_1.0.4         readxl_1.4.1        rstudioapi_0.14    
[37] farver_2.1.1        generics_0.1.3      jsonlite_1.8.0      vroom_1.5.7        
[41] googlesheets4_1.0.1 magrittr_2.0.3      s2_1.1.0            Rcpp_1.0.9         
[45] munsell_0.5.0       fansi_1.0.3         lifecycle_1.0.1     stringi_1.7.8      
[49] yaml_2.3.5          snakecase_0.11.0    MASS_7.3-57         geogrid_0.1.1      
[53] plyr_1.8.7          grid_4.2.1          parallel_4.2.1      crayon_1.5.1       
[57] lattice_0.20-45     haven_2.5.0         hms_1.1.2           knitr_1.39         
[61] pillar_1.8.1        wk_0.6.0            reprex_2.0.2        imguR_1.0.3        
[65] glue_1.6.2          evaluate_0.16       modelr_0.1.9        png_0.1-7          
[69] vctrs_0.4.1         tzdb_0.3.0          tweenr_2.0.1        cellranger_1.1.0   
[73] gtable_0.3.0        polyclip_1.10-0     reactR_0.4.4        assertthat_0.2.1   
[77] xfun_0.32           broom_1.0.0         e1071_1.7-11        rnaturalearth_0.1.0
[81] class_7.3-20        googledrive_2.0.0   gargle_1.2.0        units_0.8-0        
[85] ellipsis_0.3.2     
`
tseyanglim commented 2 months ago

Having the same issue - @ht1212 did you ever find a solution?

hafen commented 2 months ago

Thanks for bringing this back to my attention. I have issued a fix that works for top/bottom/left/right axes.

remotes::install_github("hafen/geofacet")
library(geofacet)
library(ggplot2)

state_unemp$dummy_variable_axis <-  state_unemp$rate + 15 

ggplot(state_unemp, aes(year, rate)) +
  geom_line() +
  geom_line(state_unemp, mapping=aes(x=year, y=dummy_variable_axis))+
  facet_geo(~ state, grid = "us_state_grid2", move_axes = TRUE) +
  scale_x_continuous(
    labels = function(x) paste0("'", substr(x, 3, 4)),
    sec.axis = sec_axis(transform = ~ ., name = "Dummy x axis")
  ) +
  ylab("Unemployment Rate (%)") +
  scale_y_continuous(
    sec.axis = sec_axis(transform = ~ ., name = "Dummy y axis")
  )

This should work if you install from GitHub. I want to wait until I get more user-submitted grids integrated into the package before releasing to CRAN again though.