1

I want to map the amount of inhabitants of the Netherlands in 500x500 m squares with plot_gg from the Rayshader package. Plotting 2D with ggplot works fine but plot_gg shows the edges of every 500x500 m polygon and I cannot find a way to disable/hide (not show) that. I think I have tried all arguments of plot_gg but to no success.

This is my (hopefully) reproducible code:

library(ggplot2)
library(rayshader)
library(sf)    

temp <- tempfile()
download.file("https://www.cbs.nl/-/media/cbs/dossiers/nederland-regionaal/vierkanten/500/2022-cbs_vk500_2021_v1.zip", temp)
NLD <- read_sf(utils::unzip(temp, "cbs_vk500_2021_v1.gpkg"))

NLD <- NLD[-c(99:nrow(NLD)),]                # for testing (faster)

NLD$aantal_inwoners <- ifelse(NLD$aantal_inwoners < 0, 1, NLD$aantal_inwoners)

kaart <- ggplot(NLD)+
  geom_sf(aes(fill=aantal_inwoners), colour=NA) +
  scale_fill_gradient(high="deeppink", low="black") + 
  theme_void() +
  theme(legend.position = "none")

#This second part is required to keep a white background in plot_gg (otherwise it is black). 
kaart <- kaart + theme(panel.background = element_rect(fill="white", color="white"),        
                       plot.background = element_rect(fill = "white", color="white"))

kaart

plot_gg(kaart, width = 3, height = 3, multicore = TRUE, 
        zoom = .7, theta = 0, phi = 90, windowsize = c(800, 800)) 

This is what I get from ggplot:

ggplot output of first 99 rows

And this is what plot_gg shows, I dont want the squares to be displayed:

plot gg

Those polygons do touch according to st_touches(NLD$geom) so I don't expect that to be the problem.

Output of SessionInfo():

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=English_Netherlands.utf8  LC_CTYPE=English_Netherlands.utf8    LC_MONETARY=English_Netherlands.utf8
[4] LC_NUMERIC=C                         LC_TIME=English_Netherlands.utf8    

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

other attached packages:
[1] filesstrings_3.2.3 stringr_1.5.0      sf_1.0-9           rayshader_0.32.7   ggplot2_3.4.0     

loaded via a namespace (and not attached):
 [1] rgl_0.110.2        Rcpp_1.0.9         prettyunits_1.1.1  png_0.1-8          class_7.3-20       assertthat_0.2.1   digest_0.6.31     
 [8] foreach_1.5.2      utf8_1.2.2         R6_2.5.1           e1071_1.7-12       pillar_1.8.1       rlang_1.0.6        progress_1.2.2    
[15] rstudioapi_0.14    extrafontdb_1.0    magick_2.7.3       textshaping_0.3.6  extrafont_0.18     htmlwidgets_1.6.1  munsell_0.5.0     
[22] proxy_0.4-27       compiler_4.2.2     xfun_0.36          pkgconfig_2.0.3    systemfonts_1.0.4  base64enc_0.1-3    htmltools_0.5.4   
[29] tidyselect_1.2.0   tibble_3.1.8       rayrender_0.29.0   codetools_0.2-18   fansi_1.0.3        crayon_1.5.2       dplyr_1.0.10      
[36] withr_2.5.0        grid_4.2.2         jsonlite_1.8.4     Rttf2pt1_1.3.11    gtable_0.3.1       lifecycle_1.0.3    DBI_1.1.3         
[43] magrittr_2.0.3     units_0.8-1        scales_1.2.1       KernSmooth_2.23-20 cli_3.4.1          stringi_1.7.8      farver_2.1.1      
[50] doParallel_1.0.17  terrainmeshr_0.1.0 ellipsis_0.3.2     ragg_1.2.5         generics_0.1.3     vctrs_0.5.1        iterators_1.0.14  
[57] tools_4.2.2        glue_1.6.2         rayimage_0.7.4     purrr_1.0.1        hms_1.1.2          parallel_4.2.2     fastmap_1.1.0     
[64] colorspace_2.0-3   strex_1.4.4        classInt_0.4-8     knitr_1.41        

For completeness, this is the output of the entire dataset (for inhabitants (inwoners in Dutch)). In plot_gg I get that ugly raster over it:

ggplot NL plot_gg

Jimmie
  • 11
  • 2
  • Did you solve this? I haven't had time to post an answer and doubt I will for a few days. However the problem is almost certainly with the crs. If you remove the `theme_void()` you should see the coordinates on the axes, which I expect will be lat / lon in epsg 4326 rather than the projection that best fits the Netherlands which I expect your source data to be in. The conversion means your squares will no longer exactly touch even though they do in the data. The amount will be too small to see in `ggplot` but visible in `rayshader`. – SamR Jan 15 '23 at 23:30
  • 1
    That does indeed seem to fix it (I'll confirm later today when I am able to test further). I chanched the crs to 4326 (`NLD$geom <- st_transform(NLD$geom, crs = 4326)`) and the first impression is good. – Jimmie Jan 16 '23 at 13:36
  • I do not know what I did earlier but I cannot reproduce it. I may have skipped the theme settings of panel.background and plot.background. But `st_transform` does not help. The original data is crs28992 by the way. So, no solution yet. – Jimmie Jan 16 '23 at 19:30
  • I think actually this is a bug in `rayshader`. If you do `geom_sf(fill = "deeppink", color = "deeppink")` then it works. However if you map the color or fill to the data in an `aes()` call, it seems to make the color a gradient from white to the desired color, on the z-axis, which I think stems from [this](https://github.com/tylermorganwall/rayshader/blob/master/R/plot_gg.R#L333) line in the source. This makes the border appear. Weirdly though this only happens when you set the `panel.background` to white. If I were you I'd either have a black background or raise this as a github issue. – SamR Jan 17 '23 at 11:15
  • 1
    @SamR Thanks for your help! I have created a question on github. Perhaps somebody has the answer there, or we do indeed have found a bug. – Jimmie Jan 17 '23 at 21:42
  • Great - can you post a link to the issue you raised? Spent a while trying to solve this before realising I couldn't - would be interested to see if there is a solution. – SamR Jan 18 '23 at 04:20
  • 1
    I have posted it in the Q&A section: https://github.com/tylermorganwall/rayshader/discussions/261 – Jimmie Jan 18 '23 at 11:57

0 Answers0