由于到目前为止,我阅读了很多类似的关于堆栈溢出的问题,所以如果不将ggplot2更新到开发版本,我就无法找到一个好的解决方案。
我的问题是,我有几个脚本使用arrangeGrob从单个图创建组合图。我将它们保存到一个变量中,并print这个变量和/或用ggsave保存它。由于我的许多同事定期更新包(我认为这是一件好事),所以在更新到gridExtra 2.0.0之后,我总是收到我的脚本不再工作的邮件。
我不知道如何处理这个问题,因为解决问题的新ggplot2版本还在开发中。我在堆栈溢出上发现了一个article,以删除要保存的对象是否为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)控制台上的一些输出和错误:
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> 创建了test.pdf,但它以任何方式损坏,无法打开。gtable对象也会被打印出来。所以我想这里出了点问题。
但是,正如您可以看到的,我在示例代码中找到了grid.draw函数来绘制我的组合图,但是修改后仍然不能ggsave。
我不想使用“旧”(pdf(file = "test.pdf"); grid.draw(b); dev.off())设备保存函数(如this article中所建议的那样),因为使用它们非常不舒服。
在this question中,有人确切地询问如何保存对象,但在答案中,他们只是解释使用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 发布于 2015-11-20 11:51:07
Pascal最终让我想到了检查ggplot 1.0.1和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
}
}
}
}它基本上覆盖了ggsave,并从开发版本创建了两个新函数。
在执行该函数之后,一切似乎都正常。
发布于 2015-11-21 21:55:54
作为这个不幸的过渡时期的临时解决办法,您可以重新实现以前在gridExtra中的类黑客,
class(b) <- c("arrange","ggplot", class(b))
print.arrange <- function(x) grid.draw(x)
ggsave('test.pdf', b)发布于 2019-03-13 22:09:12
我的修正是显式地定义文件:
ggsave(file='test.pdf', b)
https://stackoverflow.com/questions/33823361
复制相似问题