UpSetR
UpSetR copied to clipboard
How do you actually save the plot?
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)
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()
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?
Yes the blank page thing is a known issue: #90, try setting onefile=FALSE
in the pdf()
function.
assigning the plot to an object returns NULL
, so how do you do it ?
The correct approach is the one from @RichardJActon
pdf(file="filename.pdf", onefile=FALSE) # or other device
upset(...)
dev.off()
The onefiles=FALSE
option is not available for the image file formats (jpeg, bmp, tiff).
What can you do in these cases?
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"
.
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
Suggestion by lukaszkruk worked for me! I just saved the plot first and then used ggsave()
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)`
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
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))
Hi!
This solution worked, but... The plot should look like this:
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:
Any ideas?
@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 :/
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
for ggplotify issue, see https://github.com/hms-dbmi/UpSetR/pull/112.
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?
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¬_enriched"=0,
"not_significant&enriched_in_control"=0,
"not_significant&enriched_in_COPD"=0,
"not_significant¬_enriched"=170,
"significant_in_control&enriched_in_control"=21,
"significant_in_control&enriched_in_COPD"=0,
"significant_in_control¬_enriched"=0,
"significant_in_COPD&enriched_in_control"=0,
"significant_in_COPD&enriched_in_COPD"=33,
"significant_in_COPD¬_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()
hope it helps!
Best, Ayu
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