Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_sf: scale_*_continuous has no effect

Tags:

r

ggplot2

sf

The following use of scale_x_continuous in combination with geom_sf removes the x-axis labels completely. I assume that the specified breaks do not really exist in the ggplot object and subsequently none at all are shown.

Is this caused by the automatic transformation from EPSG 32717 to EPSG 4326 by coord_sf()?

data(ecuador, package = "sperrorest")
library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.2.2, proj.4 4.9.2
library(ggplot2)
data = st_as_sf(ecuador, coords = c("x", "y"), crs = 32717)
ggplot(data) + 
  geom_sf()

ggplot(data) + 
  geom_sf() + 
  scale_x_continuous(breaks = c(79.085, 79.055))

devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.3 (2017-11-30)
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2018-01-29
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                            
#>  backports    1.1.2      2017-12-13 CRAN (R 3.4.3)                    
#>  base       * 3.4.3      2017-12-01 local                             
#>  class        7.3-14     2015-08-30 CRAN (R 3.4.0)                    
#>  classInt     0.1-24     2017-04-16 CRAN (R 3.4.3)                    
#>  colorspace   1.3-2      2016-12-14 CRAN (R 3.4.3)                    
#>  compiler     3.4.3      2017-12-01 local                             
#>  datasets   * 3.4.3      2017-12-01 local                             
#>  DBI          0.7        2017-06-18 CRAN (R 3.4.3)                    
#>  devtools     1.13.4     2017-11-09 CRAN (R 3.4.3)                    
#>  digest       0.6.15     2018-01-28 cran (@0.6.15)                    
#>  e1071        1.6-8      2017-02-02 CRAN (R 3.4.3)                    
#>  evaluate     0.10.1     2017-06-24 CRAN (R 3.4.3)                    
#>  ggplot2    * 2.2.1.9000 2018-01-29 Github (tidyverse/ggplot2@401511e)
#>  graphics   * 3.4.3      2017-12-01 local                             
#>  grDevices  * 3.4.3      2017-12-01 local                             
#>  grid         3.4.3      2017-12-01 local                             
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.4.3)                    
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.4.3)                    
#>  knitr        1.19       2018-01-29 cran (@1.19)                      
#>  lazyeval     0.2.1      2017-10-29 CRAN (R 3.4.3)                    
#>  magrittr     1.5        2014-11-22 CRAN (R 3.4.3)                    
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.4.3)                    
#>  methods    * 3.4.3      2017-12-01 local                             
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.4.3)                    
#>  pillar       1.1.0      2018-01-14 cran (@1.1.0)                     
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.4.3)                    
#>  Rcpp         0.12.15    2018-01-20 cran (@0.12.15)                   
#>  rlang        0.1.6.9003 2018-01-29 Github (tidyverse/rlang@a8c15c6)  
#>  rmarkdown    1.8        2017-11-17 CRAN (R 3.4.3)                    
#>  rprojroot    1.3-2      2018-01-03 CRAN (R 3.4.3)                    
#>  scales       0.5.0.9000 2018-01-29 Github (hadley/scales@d767915)    
#>  sf         * 0.6-0      2018-01-06 CRAN (R 3.4.3)                    
#>  stats      * 3.4.3      2017-12-01 local                             
#>  stringi      1.1.6      2017-11-17 CRAN (R 3.4.3)                    
#>  stringr      1.2.0      2017-02-18 CRAN (R 3.4.3)                    
#>  tibble       1.4.2      2018-01-22 cran (@1.4.2)                     
#>  tools        3.4.3      2017-12-01 local                             
#>  udunits2     0.13       2016-11-17 CRAN (R 3.4.3)                    
#>  units        0.5-1      2018-01-08 CRAN (R 3.4.3)                    
#>  utils      * 3.4.3      2017-12-01 local                             
#>  withr        2.1.1.9000 2018-01-29 Github (jimhester/withr@df18523)  
#>  yaml         2.1.16     2017-12-12 CRAN (R 3.4.3)
like image 683
pat-s Avatar asked Jan 29 '18 21:01

pat-s


1 Answers

Your breaks must be negative because you are in the Western hemisphere :

ggplot(data) + 
    geom_sf() + 
    scale_x_continuous(breaks = c(-79.085, -79.055))

enter image description here

It is also easy to change the crs directly from ggplot with coord_sf :

ggplot(data) + 
    geom_sf() + 
    coord_sf(datum = st_crs(data)) +
    scale_x_continuous(breaks = c(712500, 715500))

enter image description here

like image 72
Gilles Avatar answered Oct 15 '22 04:10

Gilles