ggalt icon indicating copy to clipboard operation
ggalt copied to clipboard

Crash of plot viewer in Rstudio when using ggsave of ggplot that includes geom_xspline

Open markbneal opened this issue 4 years ago • 2 comments

The plot viewer crash occurs on my computer, and on Rstudio cloud, due to some interaction with geom_xspline() I don't know if this is a ggalt issue or a ggsave issue, or an rstudio IDE issue. In any case I've raised it at the rstudio community, at: https://community.rstudio.com/t/rstudio-plot-viewer-crashes-with-ggsave-and-ggalt-combination-desktop-and-cloud/63822 . I've also raised on ggplot github (https://github.com/tidyverse/ggplot2/issues/3974)

See cloud project here (https://rstudio.cloud/project/1219353).

#ggsave of ggplot crashes viewer

# install.packages("tidyverse")
# install.packages("ggalt")

library(tidyverse)
library(ggalt)

plot(cars)

#This Works
p <- ggplot(mtcars, aes(x=hp, y=mpg, group = cyl, colour=cyl))+
  geom_point(size=1)+
  theme(legend.position = "none")
p
ggsave("my plot 1.png", plot = p)

#This crashes
p <- ggplot(mtcars, aes(x=hp, y=mpg, group = cyl, colour=cyl))+
  geom_point(size=1)+
  theme(legend.position = "none") +
  geom_xspline(spline_shape = -0.5, size=0.5, linetype=2) #adding this line causes ggsave not to work, and crashes plot viewer
p
ggsave("my plot 2.png", plot = p)
sessionInfo()
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252    LC_MONETARY=English_New Zealand.1252
[4] LC_NUMERIC=C                         LC_TIME=English_New Zealand.1252    

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

other attached packages:
 [1] ggalt_0.4.0     forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.4     readr_1.3.1     tidyr_1.0.2    
 [8] tibble_3.0.1    ggplot2_3.3.0   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] tidyselect_1.0.0   haven_2.2.0        lattice_0.20-41    colorspace_1.4-1   vctrs_0.2.4        generics_0.0.2    
 [7] rlang_0.4.5        pillar_1.4.3       glue_1.4.0         withr_2.2.0        DBI_1.1.0          dbplyr_1.4.3      
[13] RColorBrewer_1.1-2 modelr_0.1.6       readxl_1.3.1       lifecycle_0.2.0    munsell_0.5.0      gtable_0.3.0      
[19] cellranger_1.1.0   rvest_0.3.5        labeling_0.3       extrafont_0.17     fansi_0.4.1        Rttf2pt1_1.3.8    
[25] broom_0.5.6        Rcpp_1.0.4.6       KernSmooth_2.23-16 scales_1.1.0       backports_1.1.6    jsonlite_1.6.1    
[31] farver_2.0.3       fs_1.4.1           proj4_1.0-10       digest_0.6.25      hms_0.5.3          packrat_0.5.0     
[37] stringi_1.4.6      ash_1.0-15         grid_4.0.0         cli_2.0.2          tools_4.0.0        magrittr_1.5      
[43] maps_3.3.0         extrafontdb_1.0    crayon_1.3.4       pkgconfig_2.0.3    ellipsis_0.3.0     MASS_7.3-51.5     
[49] xml2_1.3.1         reprex_0.3.0       lubridate_1.7.8    assertthat_0.2.1   httr_1.4.1         rstudioapi_0.11   
[55] R6_2.4.1           nlme_3.1-147       compiler_4.0.0  

markbneal avatar Apr 30 '20 02:04 markbneal

I've done some additional testing with the code from geom_xspline() help file to identify:

  1. Exactly which options in geom_xspline() are causing issues with ggsave() -> linetype=2 and spline_shape = -0.5 are problems, a simple geom_xspline(size=0.5) works with ggsave()
  2. Alternative save plot methods and whether they work -> cowplot save doesn't. base png() and pdf() work, even with options that are problematic:
#from help for geom_xspline ############################################################
library(ggalt)
library(cowplot)

set.seed(1492)
dat <- data.frame(x=c(1:10, 1:10, 1:10),
                  y=c(sample(15:30, 10), 2*sample(15:30, 10),
                      3*sample(15:30, 10)),
                  group=factor(c(rep(1, 10), rep(2, 10), rep(3, 10)))
)

ggplot(dat, aes(x, y, group=group, color=group)) +
  geom_point() +
  geom_line()

#works
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
  geom_point() +
  geom_line() +
  geom_smooth(se=FALSE, linetype="dashed", size=0.5)

ggsave("my plot 5.png")

#doesn't ggsave, depending on geom_xspline parameters
p1 <- ggplot(dat, aes(x, y, group=group, color=factor(group))) +
  geom_point(color="black") +
  geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
#  geom_xspline(size=0.5)                         #will save with ggsave - this is the version in help
#  geom_xspline(linetype=2)                       #crashes with ggsave
geom_xspline(spline_shape = -0.5)                 #crashes with ggsave
p1

#ggsave("my plot 6.png")

#save_plot("p1.png", p1) #using cowplot save, also doesn't work


#works
#http://www.sthda.com/english/wiki/creating-and-saving-graphs-r-base-graphs
pdf("my plot 9.pdf") 
# 2. Create a plot
p1            
# Close the pdf file
dev.off() 

#works
png("my plot 10.png")
p1
dev.off() 

markbneal avatar Apr 30 '20 23:04 markbneal

Issue at ggplot can be reopened if it is clear issue is at that end of the chain. Obviously we can sidestep the problem temporarily by saving with png(), but it would be nice to solve.

markbneal avatar Apr 30 '20 23:04 markbneal