drm*_*iod 9 r ggplot2 gridextra
因为到目前为止我在stackoverflow上读了很多类似的问题,所以在没有将ggplot2更新到开发版本的情况下我找不到一个好的解决方案.
我的问题是,我有几个脚本用于arrangeGrob从单个图形中创建组合图形.我将它们保存到变量和print这个变量中和/或保存它ggsave.由于我的很多同事经常更新包(这是我认为的好事),我总是收到邮件,我的脚本在更新后不再有效gridExtra 2.0.0.
我不知道如何处理这个,因为ggplot2问题解决的新版本仍在开发中.我找到一篇关于堆栈溢出的文章,如果要保存的对象是一个,ggplot因为新arrangeGrob函数返回一个gtable对象,就删除了一个测试,但是在我的情况下这个失败了:
library(ggplot2)
library(grid)
library(gridExtra)
a <- data.frame(x=c(1,2,3),
y=c(2,3,4))
p <- ggplot(a, aes(x, y)) + geom_point()
b <- arrangeGrob(p, p)
grid.draw(b)
ggsave('test.pdf', b)
ggsave <- ggplot2::ggsave
body(ggsave) <- body(ggplot2::ggsave)[-2]
ggsave('test.pdf', b)
Run Code Online (Sandbox Code Playgroud)
控制台上的一些输出和错误:
d> grid.draw(b)
d> ggsave('test.pdf', b)
Error in ggsave("test.pdf", b) : plot should be a ggplot2 plot
d> ggsave <- ggplot2::ggsave
d> body(ggsave) <- body(ggplot2::ggsave)[-2]
d> ggsave('test.pdf', b)
Saving 10.5 x 10.7 in image
TableGrob (2 x 1) "arrange": 2 grobs
z cells name grob
1 1 (1-1,1-1) arrange gtable[layout]
2 2 (2-2,1-1) arrange gtable[layout]
d>
Run Code Online (Sandbox Code Playgroud)
在test.pdf被创建,但它以任何方式损坏,无法打开.另外,gtable对象获取打印.所以我猜这里有些不对劲.
但是,正如你所看到的,我在示例代码中发现,我发现grid.draw函数至少可以绘制我的组合图,但ggsave在修改之后我仍然不能.
我不想使用本文中pdf(file = "test.pdf"); grid.draw(b); dev.off()建议的"旧"()设备保存功能,因为它们使用起来非常不舒服.
在这个问题中,有人问到如何保存对象,但在答案中他们只是解释使用grid.darw,他接受了答案,solving the problem到目前为止没有人回答我的评论.
所以我现在很迷失,如何为那些已经和没有更新到新gridExtra包的人提供工作脚本.在ggsave函数中删除测试的方法是我认为最好的解决方案,因为我可以检查gridExtra和ggplot2版本,只是ggsave在版本不匹配的情况下覆盖函数,但我无法运行它.
期待得到一些帮助.
编辑:
也许是sessionInfo帮助
d> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] gridExtra_2.0.0 ggplot2_1.0.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.1 digest_0.6.8 MASS_7.3-44 plyr_1.8.3 gtable_0.1.2
[6] magrittr_1.5 scales_0.3.0 stringi_1.0-1 reshape2_1.4.1 devtools_1.9.1
[11] proto_0.3-10 tools_3.2.0 stringr_1.0.0 munsell_0.4.2 colorspace_1.2-6
[16] memoise_0.2.1
Run Code Online (Sandbox Code Playgroud)
ggplot 1.0.1Pascal 最终让我想到了检查和之间的差异的想法ggplot 1.0.1.9003,因为我不想或强制使用ggplot.
所以我的想法是一个将在每个脚本中执行的函数,该函数会覆盖默认ggsave函数。
我现在测试了一下,如果有任何错误或其他问题,请告诉我。但我现在的做法到目前为止还有效。
repairGgsave <- function() {
ggplot_version <-
compareVersion(as.character(packageVersion('ggplot2')),
'1.0.1.9003')
gridextra_version <-
compareVersion(as.character(packageVersion('gridExtra')),
'0.9.1')
if(gridextra_version > 0) {
if(ggplot_version <= 0) {
ggsave <- function(filename, plot = last_plot(),
device = NULL, path = NULL, scale = 1,
width = NA, height = NA, units = c("in", "cm", "mm"),
dpi = 300, limitsize = TRUE, ...) {
dev <- plot_dev(device, filename, dpi = dpi)
dim <- plot_dim(c(width, height), scale = scale, units = units,
limitsize = limitsize)
if (!is.null(path)) {
filename <- file.path(path, filename)
}
dev(file = filename, width = dim[1], height = dim[2], ...)
on.exit(utils::capture.output(grDevices::dev.off()))
grid.draw(plot)
invisible()
}
assign("ggsave", ggsave, .GlobalEnv)
plot_dim <<- function(dim = c(NA, NA), scale = 1, units = c("in", "cm", "mm"),
limitsize = TRUE) {
units <- match.arg(units)
to_inches <- function(x) x / c(`in` = 1, cm = 2.54, mm = 2.54 * 10)[units]
from_inches <- function(x) x * c(`in` = 1, cm = 2.54, mm = 2.54 * 10)[units]
dim <- to_inches(dim) * scale
if (any(is.na(dim))) {
if (length(grDevices::dev.list()) == 0) {
default_dim <- c(7, 7)
} else {
default_dim <- dev.size() * scale
}
dim[is.na(dim)] <- default_dim[is.na(dim)]
dim_f <- prettyNum(from_inches(dim), digits = 3)
message("Saving ", dim_f[1], " x ", dim_f[2], " ", units, " image")
}
if (limitsize && any(dim >= 50)) {
stop("Dimensions exceed 50 inches (height and width are specified in '",
units, "' not pixels). If you're sure you a plot that big, use ",
"`limitsize = FALSE`.", call. = FALSE)
}
dim
}
plot_dev <<- function(device, filename, dpi = 300) {
if (is.function(device))
return(device)
eps <- function(...) {
grDevices::postscript(..., onefile = FALSE, horizontal = FALSE,
paper = "special")
}
devices <- list(
eps = eps,
ps = eps,
tex = function(...) grDevices::pictex(...),
pdf = function(..., version = "1.4") grDevices::pdf(..., version = version),
svg = function(...) grDevices::svg(...),
emf = function(...) grDevices::win.metafile(...),
wmf = function(...) grDevices::win.metafile(...),
png = function(...) grDevices::png(..., res = dpi, units = "in"),
jpg = function(...) grDevices::jpeg(..., res = dpi, units = "in"),
jpeg = function(...) grDevices::jpeg(..., res = dpi, units = "in"),
bmp = function(...) grDevices::bmp(..., res = dpi, units = "in"),
tiff = function(...) grDevices::tiff(..., res = dpi, units = "in")
)
if (is.null(device)) {
device <- tolower(tools::file_ext(filename))
}
if (!is.character(device) || length(device) != 1) {
stop("`device` must be NULL, a string or a function.", call. = FALSE)
}
dev <<- devices[[device]]
if (is.null(dev)) {
stop("Unknown graphics device '", device, "'", call. = FALSE)
}
dev
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它基本上覆盖了ggsave开发版本并创建了两个新函数。
执行该函数后,一切似乎都正常。