ggsave option for ggbio Tracks function
2
0
Entering edit mode
Mark Dunning ★ 1.1k
@mark-dunning-3319
Last seen 21 months ago
Sheffield, Uk
Hi, I was just wondering if there was an option to save a 'Tracks' object to disk using a ggsave-like command? > p<- tracks(p.ideo, Gene = p1,Probes=p2,main=gene,heights=c(1,5,3)) > ggsave(p,file="tracks.png") Error in ggsave(p, file = "tracks.png") : plot should be a ggplot2 plot Regards, Mark
• 3.1k views
ADD COMMENT
0
Entering edit mode
Tengfei Yin ▴ 420
@tengfei-yin-4323
Last seen 8.6 years ago
Hi Mark, This is a very good suggestion, you are right, ggsave doesn't work for tracks object now, and that's very annoying. For now, you can simply remove the check command(first two lines) in ggsave() and make sure any object could be saved, that works, if you pass a tracks object in the function. But won't work if you simply run ggsave without tracks name, because by default, last_plot() only capture single plot. Below is a sample code, you can run it and use it just for now, I am thinking about implementing in a more general way in ggbio devel and will let you know later . ggbiosave <- function (filename = default_name(plot), plot = last_plot(), device = default_device(filename), path = NULL, scale = 1, width = par("din")[1], height = par("din")[2], units = c("in", "cm", "mm"), dpi = 300, limitsize = TRUE, ...) { ## simply comment out the check part ## if (!inherits(plot, "ggplot")) ## stop("plot should be a ggplot2 plot") eps <- ps <- function(..., width, height) grDevices::postscript(..., width = width, height = height, onefile = FALSE, horizontal = FALSE, paper = "special") tex <- function(..., width, height) grDevices::pictex(..., width = width, height = height) pdf <- function(..., version = "1.4") grDevices::pdf(..., version = version) svg <- function(...) grDevices::svg(...) wmf <- function(..., width, height) grDevices::win.metafile(..., width = width, height = height) emf <- function(..., width, height) grDevices::win.metafile(..., width = width, height = height) png <- function(..., width, height) grDevices::png(..., width = width, height = height, res = dpi, units = "in") jpg <- jpeg <- function(..., width, height) grDevices::jpeg(..., width = width, height = height, res = dpi, units = "in") bmp <- function(..., width, height) grDevices::bmp(..., width = width, height = height, res = dpi, units = "in") tiff <- function(..., width, height) grDevices::tiff(..., width = width, height = height, res = dpi, units = "in") default_name <- function(plot) { paste(digest.ggplot(plot), ".pdf", sep = "") } default_device <- function(filename) { pieces <- strsplit(filename, "\\.")[[1]] ext <- tolower(pieces[length(pieces)]) match.fun(ext) } units <- match.arg(units) convert_to_inches <- function(x, units) { x <- switch(units, `in` = x, cm = x/2.54, mm = x/2.54/10) } convert_from_inches <- function(x, units) { x <- switch(units, `in` = x, cm = x * 2.54, mm = x * 2.54 * 10) } if (!missing(width)) { width <- convert_to_inches(width, units) } if (!missing(height)) { height <- convert_to_inches(height, units) } if (missing(width) || missing(height)) { message("Saving ", prettyNum(convert_from_inches(width * scale, units), digits = 3), " x ", prettyNum(convert_from_inches(height * scale, units), digits = 3), " ", units, " image") } width <- width * scale height <- height * scale if (limitsize && (width >= 50 || height >= 50)) { stop("Dimensions exceed 50 inches (height and width are specified in inches/cm/mm, not pixels).", " If you are sure you want these dimensions, use 'limitsize=FALSE'.") } if (!is.null(path)) { filename <- file.path(path, filename) } device(file = filename, width = width, height = height, ...) on.exit(capture.output(dev.off())) print(plot) invisible() } Thanks Tengfei On Thu, Jan 10, 2013 at 8:22 AM, Mark Dunning <mark.dunning@gmail.com>wrote: > Hi, > > I was just wondering if there was an option to save a 'Tracks' object > to disk using a ggsave-like command? > > > p<- tracks(p.ideo, Gene = p1,Probes=p2,main=gene,heights=c(1,5,3)) > > ggsave(p,file="tracks.png") > Error in ggsave(p, file = "tracks.png") : plot should be a ggplot2 plot > > Regards, > > Mark > > _______________________________________________ > Bioconductor mailing list > Bioconductor@r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > -- Tengfei Yin MCDB PhD student 1620 Howe Hall, 2274, Iowa State University Ames, IA,50011-2274 [[alternative HTML version deleted]]
ADD COMMENT
0
Entering edit mode
a little more update, in the svn, I commit the change to override ggsave to let it support ggplot and Tracks object, also I let tracks has the ability to set itself as last plot too, so in next release, the ggsave function should work seamless for tracks or ggbio's plots. Tengfei On Thu, Jan 10, 2013 at 11:28 AM, Tengfei Yin <yintengfei@gmail.com> wrote: > Hi Mark, > > This is a very good suggestion, you are right, ggsave doesn't work for > tracks object now, and that's very annoying. > > For now, you can simply remove the check command(first two lines) in > ggsave() and make sure any object could be saved, that works, if you pass a > tracks object in the function. But won't work if you simply run ggsave > without tracks name, because by default, last_plot() only capture single > plot. Below is a sample code, you can run it and use it just for now, I am > thinking about implementing in a more general way in ggbio devel and will > let you know later . > > > ggbiosave <- function (filename = default_name(plot), plot = last_plot(), > device = default_device(filename), path = NULL, > scale = 1, > width = par("din")[1], height = par("din")[2], > units = c("in", > > "cm", "mm"), dpi = 300, limitsize = TRUE, ...) > { > ## simply comment out the check part > ## if (!inherits(plot, "ggplot")) > ## stop("plot should be a ggplot2 plot") > eps <- ps <- function(..., width, height) grDevices::postscript(..., > width = width, height = height, onefile = FALSE, horizontal = > FALSE, > paper = "special") > tex <- function(..., width, height) grDevices::pictex(..., > width = width, height = height) > pdf <- function(..., version = "1.4") grDevices::pdf(..., > version = version) > svg <- function(...) grDevices::svg(...) > wmf <- function(..., width, height) grDevices::win.metafile(..., > width = width, height = height) > emf <- function(..., width, height) grDevices::win.metafile(..., > width = width, height = height) > png <- function(..., width, height) grDevices::png(..., width = width, > height = height, res = dpi, units = "in") > jpg <- jpeg <- function(..., width, height) grDevices::jpeg(..., > width = width, height = height, res = dpi, units = "in") > bmp <- function(..., width, height) grDevices::bmp(..., width = width, > height = height, res = dpi, units = "in") > tiff <- function(..., width, height) grDevices::tiff(..., > width = width, height = height, res = dpi, units = "in") > default_name <- function(plot) { > paste(digest.ggplot(plot), ".pdf", sep = "") > } > default_device <- function(filename) { > pieces <- strsplit(filename, "\\.")[[1]] > ext <- tolower(pieces[length(pieces)]) > match.fun(ext) > } > units <- match.arg(units) > convert_to_inches <- function(x, units) { > x <- switch(units, `in` = x, cm = x/2.54, mm = x/2.54/10) > } > convert_from_inches <- function(x, units) { > x <- switch(units, `in` = x, cm = x * 2.54, mm = x * > 2.54 * 10) > } > if (!missing(width)) { > width <- convert_to_inches(width, units) > } > if (!missing(height)) { > height <- convert_to_inches(height, units) > } > if (missing(width) || missing(height)) { > message("Saving ", prettyNum(convert_from_inches(width * > scale, units), digits = 3), " x ", > prettyNum(convert_from_inches(height * > scale, units), digits = 3), " ", units, " image") > } > width <- width * scale > height <- height * scale > if (limitsize && (width >= 50 || height >= 50)) { > stop("Dimensions exceed 50 inches (height and width are specified > in inches/cm/mm, not pixels).", > " If you are sure you want these dimensions, use > 'limitsize=FALSE'.") > } > if (!is.null(path)) { > filename <- file.path(path, filename) > } > device(file = filename, width = width, height = height, ...) > on.exit(capture.output(dev.off())) > print(plot) > invisible() > } > > Thanks > > Tengfei > > > On Thu, Jan 10, 2013 at 8:22 AM, Mark Dunning <mark.dunning@gmail.com>wrote: > >> Hi, >> >> I was just wondering if there was an option to save a 'Tracks' object >> to disk using a ggsave-like command? >> >> > p<- tracks(p.ideo, Gene = p1,Probes=p2,main=gene,heights=c(1,5,3)) >> > ggsave(p,file="tracks.png") >> Error in ggsave(p, file = "tracks.png") : plot should be a ggplot2 plot >> >> Regards, >> >> Mark >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor@r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >> > > > > -- > Tengfei Yin > MCDB PhD student > 1620 Howe Hall, 2274, > Iowa State University > Ames, IA,50011-2274 > > > -- Tengfei Yin MCDB PhD student 1620 Howe Hall, 2274, Iowa State University Ames, IA,50011-2274 [[alternative HTML version deleted]]
ADD REPLY
0
Entering edit mode
Hi Tengfei, Many thanks for your quick and helpful response. That does exactly what I want. Mark On Thu, Jan 10, 2013 at 9:44 PM, Tengfei Yin <yintengfei at="" gmail.com=""> wrote: > a little more update, in the svn, I commit the change to override ggsave to > let it support ggplot and Tracks object, also I let tracks has the ability > to set itself as last plot too, so in next release, the ggsave function > should work seamless for tracks or ggbio's plots. > > Tengfei > > > On Thu, Jan 10, 2013 at 11:28 AM, Tengfei Yin <yintengfei at="" gmail.com=""> wrote: >> >> Hi Mark, >> >> This is a very good suggestion, you are right, ggsave doesn't work for >> tracks object now, and that's very annoying. >> >> For now, you can simply remove the check command(first two lines) in >> ggsave() and make sure any object could be saved, that works, if you pass a >> tracks object in the function. But won't work if you simply run ggsave >> without tracks name, because by default, last_plot() only capture single >> plot. Below is a sample code, you can run it and use it just for now, I am >> thinking about implementing in a more general way in ggbio devel and will >> let you know later . >> >> >> ggbiosave <- function (filename = default_name(plot), plot = last_plot(), >> device = default_device(filename), path = NULL, >> scale = 1, >> width = par("din")[1], height = par("din")[2], >> units = c("in", >> >> "cm", "mm"), dpi = 300, limitsize = TRUE, ...) >> { >> ## simply comment out the check part >> ## if (!inherits(plot, "ggplot")) >> ## stop("plot should be a ggplot2 plot") >> eps <- ps <- function(..., width, height) grDevices::postscript(..., >> width = width, height = height, onefile = FALSE, horizontal = >> FALSE, >> paper = "special") >> tex <- function(..., width, height) grDevices::pictex(..., >> width = width, height = height) >> pdf <- function(..., version = "1.4") grDevices::pdf(..., >> version = version) >> svg <- function(...) grDevices::svg(...) >> wmf <- function(..., width, height) grDevices::win.metafile(..., >> width = width, height = height) >> emf <- function(..., width, height) grDevices::win.metafile(..., >> width = width, height = height) >> png <- function(..., width, height) grDevices::png(..., width = width, >> height = height, res = dpi, units = "in") >> jpg <- jpeg <- function(..., width, height) grDevices::jpeg(..., >> width = width, height = height, res = dpi, units = "in") >> bmp <- function(..., width, height) grDevices::bmp(..., width = width, >> height = height, res = dpi, units = "in") >> tiff <- function(..., width, height) grDevices::tiff(..., >> width = width, height = height, res = dpi, units = "in") >> default_name <- function(plot) { >> paste(digest.ggplot(plot), ".pdf", sep = "") >> } >> default_device <- function(filename) { >> pieces <- strsplit(filename, "\\.")[[1]] >> ext <- tolower(pieces[length(pieces)]) >> match.fun(ext) >> } >> units <- match.arg(units) >> convert_to_inches <- function(x, units) { >> x <- switch(units, `in` = x, cm = x/2.54, mm = x/2.54/10) >> } >> convert_from_inches <- function(x, units) { >> x <- switch(units, `in` = x, cm = x * 2.54, mm = x * >> 2.54 * 10) >> } >> if (!missing(width)) { >> width <- convert_to_inches(width, units) >> } >> if (!missing(height)) { >> height <- convert_to_inches(height, units) >> } >> if (missing(width) || missing(height)) { >> message("Saving ", prettyNum(convert_from_inches(width * >> scale, units), digits = 3), " x ", >> prettyNum(convert_from_inches(height * >> scale, units), digits = 3), " ", units, " image") >> } >> width <- width * scale >> height <- height * scale >> if (limitsize && (width >= 50 || height >= 50)) { >> stop("Dimensions exceed 50 inches (height and width are specified >> in inches/cm/mm, not pixels).", >> " If you are sure you want these dimensions, use >> 'limitsize=FALSE'.") >> } >> if (!is.null(path)) { >> filename <- file.path(path, filename) >> } >> device(file = filename, width = width, height = height, ...) >> on.exit(capture.output(dev.off())) >> print(plot) >> invisible() >> } >> >> Thanks >> >> Tengfei >> >> >> On Thu, Jan 10, 2013 at 8:22 AM, Mark Dunning <mark.dunning at="" gmail.com=""> >> wrote: >>> >>> Hi, >>> >>> I was just wondering if there was an option to save a 'Tracks' object >>> to disk using a ggsave-like command? >>> >>> > p<- tracks(p.ideo, Gene = p1,Probes=p2,main=gene,heights=c(1,5,3)) >>> > ggsave(p,file="tracks.png") >>> Error in ggsave(p, file = "tracks.png") : plot should be a ggplot2 plot >>> >>> Regards, >>> >>> Mark >>> >>> _______________________________________________ >>> Bioconductor mailing list >>> Bioconductor at r-project.org >>> https://stat.ethz.ch/mailman/listinfo/bioconductor >>> Search the archives: >>> http://news.gmane.org/gmane.science.biology.informatics.conductor >> >> >> >> >> -- >> Tengfei Yin >> MCDB PhD student >> 1620 Howe Hall, 2274, >> Iowa State University >> Ames, IA,50011-2274 >> >> > > > > -- > Tengfei Yin > MCDB PhD student > 1620 Howe Hall, 2274, > Iowa State University > Ames, IA,50011-2274 > >
ADD REPLY
0
Entering edit mode
kevin.rue ▴ 350
@kevinrue-6757
Last seen 6 months ago
University of Oxford

Hi,

I feel like I have to bring this subject back to the top, as I have issues ggsave-ing the output of autoplot (...).
I can see that the output of autoplot has class "GGbio", not "ggplot". Is that a big difference? It seems overkill to go and change the source code of ggbio::ggplot to remove the check as suggested in previous posts.

Essentially my code is:

gg <- autoplot(grange.object, geom = "point", coord = "genome", aes(y = mLogP))
ggsave(filename = "Manhattan.png", plot = gg, path = outfolder)

With:

> inherits(gg, "ggplot")
[1] FALSE
> class(gg)
[1] "GGbio"
attr(,"package")
[1] "ggbio"

I am using

> utils::packageVersion("ggbio")
[1] ‘1.18.5’

Many thanks in advance for advice!
Kevin

 

ADD COMMENT
0
Entering edit mode

Quick correction, ggbio::ggsave does not complain if I do not specify the "plot=" argument (which then defaults to "last_plot()").

Glad that I found a workaround, although I am not getting the logic of the function here; the help page of the ggbio::autoplot function says that the value should be: "A ggplot object, so you can use common features from ggplot2 package to manipulate the plot."

ADD REPLY

Login before adding your answer.

Traffic: 582 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6