Skip to content

Feature Request: Add 'clip' argument to coord_sf() #2938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bschneidr opened this issue Oct 12, 2018 · 6 comments · Fixed by #2942
Closed

Feature Request: Add 'clip' argument to coord_sf() #2938

bschneidr opened this issue Oct 12, 2018 · 6 comments · Fixed by #2942
Assignees

Comments

@bschneidr
Copy link

Just as coord_map() has a clip argument (e.g. coord_map(clip = 'off')), it would be useful for coord_sf() to have a clip argument, too.

I think these are the two sections of the code that would need to be updated (ignoring the roxygen comments).

ggplot2/R/sf.R

Lines 788 to 792 in 3e1e6e4

coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
crs = NULL, datum = sf::st_crs(4326),
label_graticule = waiver(),
label_axes = waiver(),
ndiscr = 100, default = FALSE) {

ggplot2/R/sf.R

Lines 825 to 835 in 3e1e6e4

ggproto(NULL, CoordSf,
limits = list(x = xlim, y = ylim),
datum = datum,
crs = crs,
label_axes = label_axes,
label_graticule = label_graticule,
ndiscr = ndiscr,
expand = expand,
default = default
)
}

@bschneidr bschneidr changed the title Add 'clip' argument to coord_sf() Feature Request: Add 'clip' argument to coord_sf() Oct 12, 2018
@clauswilke
Copy link
Member

Yes, absolutely. I'm embarrassed that I missed that one. We're too late for ggplot2 3.1.0, but this can be done for the next release.

As workaround, you can probably just assign your coord_sf() call to a variable and then set the clip attribute to "off":

crd <- coord_sf(...)
crd$clip <- "off"
ggplot() + ... + crd

I haven't tried this but I assume this will work.

@clauswilke clauswilke self-assigned this Oct 12, 2018
@bschneidr
Copy link
Author

Thanks, @clauswilke!

I can confirm that that workaround does indeed work.

crd <- coord_sf()
crd$clip <- "off"

my_plot + crd

image

@hadley
Copy link
Member

hadley commented Oct 15, 2018

@clauswilke we can probably squeeze it in - I forgot to submit last week, and I'm travelling until Thursday.

@clauswilke
Copy link
Member

@hadley I made a PR: #2942
I'm always worried that even very minor, seemingly innocuous changes may have unexpected side effects, but maybe this is a safe feature to add at the last minute.

@bschneidr Could you install the branch with the added feature and test? Let me know if you need help installing.

@bschneidr
Copy link
Author

bschneidr commented Oct 16, 2018

Beautiful! It worked like a charm.

library(ggplot2) # Installed using devtools::install_github("tidyverse/ggplot2#2942")
library(dplyr)
library(sf)
library(tigris)

# Download the Louisville-area shapefile from Census Bureau using tigris
  ky_zcta_sf <- tigris::zctas(cb = TRUE,
                              class = 'sf',
                              state = "KY", year = 2016) %>% 
    filter(startsWith(ZCTA5CE10, "402"))

# Plot the ZIP codes of Louisville
  ggplot(ky_zcta_sf) +
    geom_sf(fill = "#39499C", color = "white") +
    geom_text(x = -85.325, y = 38.15,
              label = "Out-of-bounds\ntext") +
    scale_x_continuous(limits = c(-86, -85.35)) +
    coord_sf(clip = 'off') +
    theme(plot.margin = unit(c(.1, .5, .1, .1), 'in'))

Created on 2018-10-16 by the reprex package (v0.2.1.9000)

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  tz       America/New_York            
#>  date     2018-10-16
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                             
#>  assertthat   0.2.0      2017-04-11 CRAN (R 3.5.0)                     
#>  base       * 3.5.1      2018-07-02 local                              
#>  bindr        0.1.1      2018-03-13 CRAN (R 3.4.3)                     
#>  bindrcpp   * 0.2.2      2018-03-29 CRAN (R 3.5.0)                     
#>  class        7.3-14     2015-08-30 CRAN (R 3.5.0)                     
#>  classInt     0.2-3      2018-04-16 CRAN (R 3.5.0)                     
#>  colorspace   1.3-2      2016-12-14 CRAN (R 3.5.0)                     
#>  compiler     3.5.1      2018-07-02 local                              
#>  crayon       1.3.4      2017-09-16 CRAN (R 3.5.0)                     
#>  curl         3.2        2018-03-28 CRAN (R 3.5.0)                     
#>  datasets   * 3.5.1      2018-07-02 local                              
#>  DBI          1.0.0      2018-05-02 CRAN (R 3.5.1)                     
#>  devtools     1.13.6     2018-06-27 CRAN (R 3.5.0)                     
#>  digest       0.6.18     2018-10-10 CRAN (R 3.5.1)                     
#>  dplyr      * 0.7.6      2018-06-29 CRAN (R 3.5.0)                     
#>  e1071        1.7-0      2018-07-28 CRAN (R 3.5.1)                     
#>  evaluate     0.12       2018-10-09 CRAN (R 3.5.1)                     
#>  foreign      0.8-71     2018-07-20 CRAN (R 3.5.1)                     
#>  ggplot2    * 3.0.0.9000 2018-10-16 Github (clauswilke/ggplot2@4a7d994)
#>  glue         1.3.0      2018-07-17 CRAN (R 3.5.1)                     
#>  graphics   * 3.5.1      2018-07-02 local                              
#>  grDevices  * 3.5.1      2018-07-02 local                              
#>  grid         3.5.1      2018-07-02 local                              
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.5.0)                     
#>  htmldeps     0.1.1      2018-07-23 Github (rstudio/htmldeps@c1023e0)  
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.5.0)                     
#>  httr         1.3.1      2017-08-20 CRAN (R 3.5.0)                     
#>  knitr        1.20       2018-02-20 CRAN (R 3.5.0)                     
#>  lattice      0.20-35    2017-03-25 CRAN (R 3.5.0)                     
#>  lazyeval     0.2.1      2017-10-29 CRAN (R 3.5.0)                     
#>  magrittr     1.5        2014-11-22 CRAN (R 3.5.0)                     
#>  maptools     0.9-4      2018-09-19 CRAN (R 3.5.1)                     
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.3.3)                     
#>  methods    * 3.5.1      2018-07-02 local                              
#>  mime         0.6        2018-10-05 CRAN (R 3.5.1)                     
#>  munsell      0.5.0      2018-06-12 CRAN (R 3.4.4)                     
#>  pillar       1.3.0      2018-07-14 CRAN (R 3.5.1)                     
#>  pkgconfig    2.0.2      2018-08-16 CRAN (R 3.5.1)                     
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.5.0)                     
#>  purrr        0.2.5      2018-05-29 CRAN (R 3.5.0)                     
#>  R6           2.3.0      2018-10-04 CRAN (R 3.5.1)                     
#>  rappdirs     0.3.1      2016-03-28 CRAN (R 3.5.0)                     
#>  Rcpp         0.12.19    2018-10-01 CRAN (R 3.5.1)                     
#>  rgdal        1.3-4      2018-08-03 CRAN (R 3.5.1)                     
#>  rlang        0.2.2      2018-08-16 CRAN (R 3.5.1)                     
#>  rmarkdown    1.10.14    2018-10-16 Github (rstudio/rmarkdown@dcc7d37) 
#>  scales       1.0.0      2018-08-09 CRAN (R 3.5.1)                     
#>  sf         * 0.6-3      2018-05-17 CRAN (R 3.5.0)                     
#>  sp           1.3-1      2018-06-05 CRAN (R 3.5.0)                     
#>  spData       0.2.9.4    2018-09-15 CRAN (R 3.5.1)                     
#>  stats      * 3.5.1      2018-07-02 local                              
#>  stringi      1.2.4      2018-07-20 CRAN (R 3.5.1)                     
#>  stringr      1.3.1      2018-05-10 CRAN (R 3.5.0)                     
#>  tibble       1.4.2      2018-01-22 CRAN (R 3.5.0)                     
#>  tidyselect   0.2.5      2018-10-11 CRAN (R 3.5.1)                     
#>  tigris     * 0.7        2018-04-14 CRAN (R 3.4.4)                     
#>  tools        3.5.1      2018-07-02 local                              
#>  units        0.6-1      2018-09-21 CRAN (R 3.5.1)                     
#>  utils      * 3.5.1      2018-07-02 local                              
#>  uuid         0.1-2      2015-07-28 CRAN (R 3.5.0)                     
#>  withr        2.1.2      2018-03-15 CRAN (R 3.5.0)                     
#>  xml2         1.2.0      2018-01-24 CRAN (R 3.5.0)                     
#>  yaml         2.2.0      2018-07-25 CRAN (R 3.5.1)

@lock
Copy link

lock bot commented Apr 21, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Apr 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants