Search code examples
rggplot2geom-raster

geom_raster not working for more than 2^15 value in the x scale


It seems that geom_raster is not working on my computer with more than 2^15-1 value in the x axis. I do not understand why it is doing so. I am quite convinced that I have done such graph in the past, but I am at a loss understanding what has changed (on my computer likely) that makes the graphic fails now. Feel free to have any suggestions and thanks in advance.

Here is a code that produces the weird results on my end. The first graphic is failing (with no error though), the second one is working as intended.

library(ggplot2)
df <- expand.grid(y = 0:4, 
                  x = 1:2^(15))
df$fill <- rnorm(2^15)

# Plot 1, failing (return empty ggplot)
ggplot(df) +
  geom_raster(aes(x = x, y = y, fill = fill))

# Plot 2, working as intended
ggplot(df[1:(2^15 - 1),]) +
  geom_raster(aes(x = x, y = y, fill = fill))

Plot 1 - not working

Plot 2 - working as intended

The code was tested on my computer. Below is the output of sessionInfo(). I've tried playing with R version and ggplot2 version but nothing worked. I've asked a colleague to test the code on his computer and it was working smoothly (both plots were working) and this for both R 4.2.3 and 4.4.0 (but with a windows system). I could circumvent the issue by using geom_tile, but I would prefer using geom_raster and understanding why it is not working any more. Any suggestion would be appreciated.

R version 4.3.3 (2024-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Paris
tzcode source: system (glibc)

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

other attached packages:
[1] ggplot2_3.5.1

loaded via a namespace (and not attached):
 [1] labeling_0.4.3    utf8_1.2.4        R6_2.5.1          tidyselect_1.2.1  farver_2.1.1      magrittr_2.0.3   
 [7] gtable_0.3.5      remotes_2.5.0     glue_1.7.0        tibble_3.2.1      pkgconfig_2.0.3   generics_0.1.3   
[13] dplyr_1.1.4       lifecycle_1.0.4   cli_3.6.2         fansi_1.0.6       scales_1.3.0      grid_4.3.3       
[19] vctrs_0.6.5       withr_3.0.0       compiler_4.3.3    rstudioapi_0.16.0 tools_4.3.3       pillar_1.9.0     
[25] munsell_0.5.1     colorspace_2.1-0  rlang_1.1.3      

Solution

  • I can reproduce your results (i.e. a "blank" plot) on R 4.2.3 (amd64 architecture, OpenBSD), without any warning or error message.

    It is probably a limitation of the grDevices. If I save the plot to a PNG file using ggsave(file = '/tmp/foo.png') I get an image file with the plot. On the other hand, when using png() or Cairo() (from the Cairo package) I get a PNG file with the "blank" plot as well.