Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggsave doesn't save the plot, though it displays fine - also crashes plot viewer. #3974

Closed
markbneal opened this issue Apr 30, 2020 · 4 comments

Comments

@markbneal
Copy link

The plot viewer crash occurs on my computer in Rstudio, in the Rgui, and on Rstudio cloud, due to some interaction with geom_xspline() when ggsave() is called- the plot is not saved, even though it displays fine.

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

#ggsave of ggplot crashes viewer

# install.packages("tidyverse")
# install.packages("ggalt")
# library("devtools")
# install_github("hrbrmstr/ggalt",ref="v0.1.1")
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)
# ggsave("my plot 1.pdf", plot = p)
# ggsave("my plot 1.jpeg", plot = p)
# ggsave("my plot 1.svg", 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)
# ggsave("my plot 2.pdf", plot = p)
# ggsave("my plot 2.jpeg", plot = p)
# ggsave("my plot 2.svg", plot = p)

sessionInfo()
@thomasp85
Copy link
Member

Since this seems to be an issue with a ggalt extension I'd suggest you open the issue there. If it turns out that this is indeed an issue from ggplot2 you are welcome to reopen

@markbneal
Copy link
Author

markbneal commented Apr 30, 2020

I have raised at ggalt (hrbrmstr/ggalt#70)
It still isn't clear to me which is problematic - possibly both?

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 for ggsave - so does that imply there is an issue for ggsave?

#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() 

@thomasp85
Copy link
Member

This is a ggalt problem, ggsave isn’t only a wrapper around opening and closing a graphic device

@trantor27
Copy link

I have the same problem with ggsave() using ggpattern::geom_rect_pattern() in the plot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants