ggsave() not working from my Rscript but working from RStudio

61 views Asked by At

Here is my reproducible problem: The test script:

library(ggplot2)

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
                      color = Species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  xlab("Sepal Length") +
  ylab("Sepal Width") +
  scale_x_continuous(limits = c(4, 8), breaks = seq(4, 8, 0.5)) +
  scale_y_continuous(limits = c(2, 5), breaks = 2:5)

ggsave(filename = file.path(getwd(), "iris.png"), plot = p, device = "png")

When I run each line it from my RStudio, everything works fine. Plot is saved where is supposed to. However, when I run it from my command line (Linux Mint, bash, (R) version 4.3.1 (2023-06-16)) I get the following error:

$ Rscript Code/test_scriptAndPlots.R 
Saving 7 x 7 in image
Error in f(...) : Graphics API version mismatch
Calls: ggsave -> dev -> png_dev -> f
Execution halted

Here is my Session Info for this test after running successfully from my RStudio:

R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 20.1

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

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

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

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

other attached packages:
[1] ggplot2_3.4.4

loaded via a namespace (and not attached):
 [1] vctrs_0.6.4       nlme_3.1-163      cli_3.6.1         rlang_1.1.2       generics_0.1.3    textshaping_0.3.7
 [7] glue_1.6.2        colorspace_2.1-0  ragg_1.2.6        scales_1.2.1      fansi_1.0.5       grid_4.3.1       
[13] munsell_0.5.0     tibble_3.2.1      lifecycle_1.0.3   compiler_4.3.1    dplyr_1.1.3       pkgconfig_2.0.3  
[19] mgcv_1.9-0        rstudioapi_0.15.0 farver_2.1.1      lattice_0.22-5    systemfonts_1.0.5 R6_2.5.1         
[25] tidyselect_1.2.0  utf8_1.2.4        pillar_1.9.0      splines_4.3.1     magrittr_2.0.3    Matrix_1.6-1.1   
[31] tools_4.3.1       withr_2.5.2       gtable_0.3.4 

What I have tried: I have tried already to re-install the ragg library as proposed in this entry, but to no avail.

What I am expecting: That my script will run from the command line when I use Rscript, saving the graphic, as it does when I run it line by line from RStudio.

Any idea? Thank you in advance for your time!

UPDATE I had to install a couple of libraries I needed for another analysis. These were : protr, distributional. quadprog, ggdist, gghalves, readODS, bayestestR, correlation, datawizards, effectsize, insight.

On of those repaired the issue, because I do not have any problem with the original code or the test I have presented here. On of those mysteries of computers, I suppose.

0

There are 0 answers