[BioC] ggsave option for ggbio Tracks function

Mark Dunning mark.dunning at gmail.com
Fri Jan 11 09:07:46 CET 2013


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



More information about the Bioconductor mailing list