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

How do you actually save the plot? #115

Open
achamess opened this issue May 5, 2018 · 19 comments
Open

How do you actually save the plot? #115

achamess opened this issue May 5, 2018 · 19 comments

Comments

@achamess
Copy link

achamess commented May 5, 2018

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)

@RichardJActon
Copy link
Contributor

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

@karaesmen
Copy link

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?

@RichardJActon
Copy link
Contributor

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

@thierrygosselin
Copy link

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

@adriaaula
Copy link

The correct approach is the one from @RichardJActon

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

@agilebean
Copy link

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

@swvanderlaan
Copy link

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".

@lukaszkruk
Copy link

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

@kprnjak
Copy link

kprnjak commented May 30, 2020

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

@liuyang2006
Copy link

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)`

@lukaszkruk
Copy link

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

@kentkr
Copy link

kentkr commented Feb 9, 2022

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

@witszymanski
Copy link

witszymanski commented Mar 25, 2022

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?

@kentkr
Copy link

kentkr commented Mar 25, 2022

@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 :/

@witszymanski
Copy link

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

@GuangchuangYu
Copy link

for ggplotify issue, see #112.

@edmcman
Copy link

edmcman commented Oct 4, 2022

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?

@ayusyarif
Copy link

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

@nlichti
Copy link

nlichti commented Feb 3, 2023

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

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