Ggplot2 Ggsave Function Causes Graphics Device to Not Display Plots

ggplot2 ggsave function causes graphics device to not display plots

Seems to be an issue with RStudio. The issue is described here and here.

The workaround is to install RStudio V0.97.311, or ensure that you execute the ggplot and ggsave lines separately (i.e. don't select and run together in a block with control+enter).

ggplot plots in scripts do not display in Rstudio

I recently happened on this question and realized that the most up to date way is to call show(p) after creating the plot.

ggsave without saving only last plot

ggsave default plot argument is last_plot(), you can change it and give your saved plot.

library(gridExtra)
temp <- grid.arrange(qplot(1:10,1:10),qplot(1:10,1:10))
ggsave(file='random.png', temp)

Why does this R ggplot2 code bring up a blank display device?

Some grid grobs have units that can only be resolved at drawing time, that is to say once a device window is open. This is the case of text grobs, for instance, as their size can depend (in the most general case) of the cex and fontsize arguments of the parent(s) viewports (which can be nested, etc.)

library(grid)
widthDetails(textGrob("hi"))

The current version of ggplot2 appears to use widthDetails in the code to build the legend grobs (guides_build function). It is conceivable that this could be replaced by grobWidth, unless the grob size is too convoluted.

Reset graph at the end of the loop :could not find function device error

You can leave out the dev.new() and jpg() commands, and also your arguments to ggsave() are incorrect. This should work:

n <- unique(wide_data$Product.Code)[1:3]
for (i in n) {
my.prod2 <- filter(tall_bind, Product.Code == i, Date > ymd("2012/04/01"))
mypath <- file.path("C:","R","SAVEHERE",paste("myplot_", i, ".jpg", sep = ""))
mytitle = paste("Plot for product", i)
p <- qplot(Date, Sold, data = my.prod2, geom = "line", main=mytitle, group = Model, colour = Model) + facet_grid(Model ~ .)
ggsave(filename = mypath, plot = p)
}

What you did was creating a new default graphics device, typically a plotting window, then a jpeg graphics device, i.e. a file. Then you tried to make ggplot2 to plot to directly to file using ggsave, i.e. using its own (jpg) device, and not using either of the two graphics devices you created.

The error, however, was because you gave ggsave the wrong arguments. But even with the right arguments, you would still have ended up with additional unused graphics windows and files through the dev.new() and jpeg() commands. I suggest some extra reading of the help (e.g. type ?ggsave at the r console).

Typically, when using ggplot2 you do not need to worry about dev.new, jpeg and the like. qplot or ggplot and ggsave should do all you need.

Scaling of plot in ggsave() different when RStudio plot pane is active

Expanding on Andrey Kolyadin's comment above...

From ggsave() documentation 'width, height Plot size in units ("in",
"cm", or "mm"). If not supplied, uses the size of current graphics
device.'

If we dig into the code behind ggsave, there's a snippet that says:

if (any(is.na(dim))) {
if (length(grDevices::dev.list()) == 0) {
default_dim <- c(7, 7)
}
else {
default_dim <- grDevices::dev.size() * scale
}
...

Which means that if the dimensions are not specified by the user:

  1. if there's no active graphics device at all, default dimensions are 7 x 7;

  2. if there's at least one active graphics device, use the dimensions of the last one (multiplied by scale, which defaults to 1).

RStudio's plot pane is a graphics device. So ggsave's behaviour differs depending on whether there's anything there.



Related Topics



Leave a reply



Submit