UpSetR icon indicating copy to clipboard operation
UpSetR copied to clipboard

How do you actually save the plot?

Open achamess opened this issue 6 years ago • 19 comments

I know this is a noob question, but how do you actually get the output of the plot? I try ggsave and I get

I save the plot to a variable, upset_plot then

ggsave("plot.pdf",upset_plot)

Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "upset"
4.
grid.draw(plot)
3.
(function (filename, plot = last_plot(), device = NULL, path = NULL, scale = 1, width = NA, height = NA, units = c("in", "cm", "mm"), dpi = 300, limitsize = TRUE, ...) { ...
2.
do.call(ggplot2::ggsave, args, envir = parent.frame())
1.
ggsave("plot.pdf", upset_plot)

achamess avatar May 05 '18 19:05 achamess

The plots can be saved with the conventional R graphics form:

pdf(file="filename.pdf") # or other device
upset(...)
dev.off()

or if you have the plot in a object

plotObject <- upset(...)
pdf(file="filename.pdf") # or other device
plotObject
dev.off()

RichardJActon avatar May 06 '18 00:05 RichardJActon

yes the usual R graphics works, but I'm getting an empty first page (followed by the second page with the plot) when I save it as pdf on Ubuntu 16.04... Any thoughts?

karaesmen avatar May 08 '18 19:05 karaesmen

Yes the blank page thing is a known issue: #90, try setting onefile=FALSE in the pdf() function.

RichardJActon avatar May 08 '18 23:05 RichardJActon

assigning the plot to an object returns NULL, so how do you do it ?

thierrygosselin avatar Jun 13 '18 04:06 thierrygosselin

The correct approach is the one from @RichardJActon

pdf(file="filename.pdf", onefile=FALSE) # or other device
upset(...)
dev.off()

adriaaula avatar Jul 18 '18 16:07 adriaaula

The onefiles=FALSE option is not available for the image file formats (jpeg, bmp, tiff). What can you do in these cases?

agilebean avatar May 19 '19 11:05 agilebean

So you can't use ggsave with lastplot() option from ggpubr @adriaaula ?

I tried to, but get: Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "upset".

swvanderlaan avatar Mar 19 '20 16:03 swvanderlaan

I'm hitting this problem when I'm piping the plot into ggsave, but it works correctly without the pipe.

bad: ggplot() %>% ggsave() good: ggplot() ggsave()

hope this helps

lukaszkruk avatar Mar 25 '20 07:03 lukaszkruk

Suggestion by lukaszkruk worked for me! I just saved the plot first and then used ggsave()

kprnjak avatar May 30 '20 00:05 kprnjak

Suggestion by lukaszkruk worked for me! I just saved the plot first and then used ggsave()

Can you provide a sample for save PDF?

When I save first time it always failed (after saveing, can not open PDF, said damaged). I then use RStudio, save again. It work. Very strange. What's wrong with following code to save PDF?

`p1 <- upset(fromExpression(fit1$original.values), scale.intersections = "log10", sets.x.label = "Total CpGs", mainbar.y.label = "Intersection CpGs", show.numbers = 'yes', text.scale = 1.5, number.angles = 30, sets.bar.color = "grey", set_size.show = TRUE, order.by = "freq")

pdf(file = outfn, onefile = FALSE) # or other device

p1 print("Start saving") dev.off() printf('save to %s', outfn)`

liuyangzzu avatar Apr 27 '21 01:04 liuyangzzu

Can you provide a sample for save PDF?

@liuyangzzu , please try:

library(ggplot2)
ggplot(data=mtcars, aes(x=disp)) + geom_histogram()
ggsave(filename = "plot.pdf")

this should write a pdf to your working directory

lukaszkruk avatar Apr 27 '21 08:04 lukaszkruk

Another way to use ggsave() is to convert the plot into ggplot object using the ggplotify function as.ggplot(). Although this produces an awkard black box for me using jpeg and cairo.

upsetPlot <- upset(...)
ggsave('path', ggplotify::as.ggplot(upsetPlot))

kentkr avatar Feb 09 '22 19:02 kentkr

Hi! This solution worked, but... The plot should look like this: image

However, after running:

ggplot2::ggsave(ggplotify::as.ggplot(p4_upseT_samples),
                filename = fs::path("Plot-UpSet_precursors.png"),
                device = "png",
                dpi = 400, units = "cm",
                height = 3 * 5.5, width = 4.5 * 5)

I get: image

Any ideas?

witszymanski avatar Mar 25 '22 13:03 witszymanski

@witszymanski that is what I meant by

this produces an awkard black box for me using jpeg and cairo.

Treat it as a work around and not a perfect solution :/

kentkr avatar Mar 25 '22 13:03 kentkr

I guess it has something to do with the structure of the plot. That the one on the left is a separate plot embedded into the other one. Some hints here: https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html I'll try to make it work in the free time. If you figure it out earlier, let us know! Greets

witszymanski avatar Mar 25 '22 13:03 witszymanski

for ggplotify issue, see https://github.com/hms-dbmi/UpSetR/pull/112.

GuangchuangYu avatar Apr 12 '22 15:04 GuangchuangYu

Creating a file:

library(UpSetR)

pdf("what.pdf", onefile=FALSE)
example(upset)
dev.off()

and running Rscript file.R produces a PDF containing no pages. Is there actually a way to save these plots?

edmcman avatar Oct 04 '22 16:10 edmcman

Hi,

Just in case somebody still needs a better solution. I attach my code here to produce better resolution with png, tiff, and jpeg

input <- c( 
  significant_in_COPD =33,
    significant_in_control=21,
    significant_in_COPD_control=82,
    not_significant=170,
    enriched_in_COPD=91,
    enriched_in_control=45,
    not_enriched=170,
    "significant_in_COPD_control&enriched_in_control"=24,
    "significant_in_COPD_control&enriched_in_COPD"=58,
    "significant_in_COPD_control&not_enriched"=0,
    "not_significant&enriched_in_control"=0,
    "not_significant&enriched_in_COPD"=0,
    "not_significant&not_enriched"=170,
    "significant_in_control&enriched_in_control"=21,
    "significant_in_control&enriched_in_COPD"=0,
    "significant_in_control&not_enriched"=0,
    "significant_in_COPD&enriched_in_control"=0,
    "significant_in_COPD&enriched_in_COPD"=33,
    "significant_in_COPD&not_enriched"=0
    )

png(paste0(Sys.Date(),"upset.png"), units="in", width=8, height=8, res=300)
upset(fromExpression(input), 
      nintersects = 12, 
      nsets = 7, 
      order.by = "freq", 
      decreasing = T, 
      mb.ratio = c(0.6, 0.4),
      number.angles = 0, 
      text.scale = 1.1, 
      point.size = 2.8, 
      line.size = 1,
      queries = list(list(query = intersects, params = list("significant_in_COPD", 
                                                            "enriched_in_COPD"), color = "darkred", active = T), 
                     list(query = intersects, params = list("significant_in_COPD"), color = "darkred", active = T), 
                     list(query = intersects, params = list("enriched_in_COPD"),color = "darkred", active = T),
                     list(query = intersects, params = list("significant_in_control", 
                                                            "enriched_in_control"), color = "steelblue4", active = T), 
                     list(query = intersects, params = list("significant_in_control"), color = "steelblue4", active = T), 
                     list(query = intersects, params = list("enriched_in_control"),color = "steelblue4", active = T))
)
dev.off()

2022-11-30upset

hope it helps!

Best, Ayu

ayusyarif avatar Nov 30 '22 09:11 ayusyarif

If you are saving inside a function, use the standard graphics devices, but explicitly print the plot. I've tested this with png and pdf - no need for onepage = FALSE - and assume the others will work as well.

a_list <- list(A = letters[1:10], B = letters[5:20], C = c("a","j","m","z"))

this_function_fails <- function(x, file = "upset_plot.png"){
  my_plot <- UpSetR::upset(UpSetR::fromList(x))
  png(file, units = "in", height = 6, width = 6.5, res = 300)
  my_plot
  dev.off()
}
this_function_fails(a_list)
shell(shQuote("upset_plot.png"))  # Blank

this_function_works <- function(x, file = "upset_plot.png"){
  my_plot <- UpSetR::upset(UpSetR::fromList(x))
  png(file, units = "in", height = 6, width = 6.5, res = 300)
  print(my_plot)                               ## use print here
  dev.off()
}
this_function_works(a_list)
shell(shQuote("upset_plot.png")) # Correct plot

nlichti avatar Feb 03 '23 22:02 nlichti