ggplot2 的多个图:非关闭文件和空白页
Multiple Plots with ggplot2: Non-closing files and blank pages
我在将多个 ggplot2 图存储为 pdf 时遇到一个奇怪的现象。使用 ggsave 存储时,我得到 B_ggplot.pdf OK,但是 A_ggplot.pdf 没有关闭文件(它似乎被锁定)。旧方法有时会生成空白页。似乎存在竞争条件,但我不能指责它。
谁能帮我理解一下?
这里是代码:
library(ggplot2)
# plot A
figureA <- ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
# plot B
figureB <- ggplot(data=mtcars, aes(x=wt, y=carb)) + geom_point()
# store PDFs using ggplot2
ggsave(file="D:/A_ggplot.pdf") # OK
ggsave(file="D:/B_ggplot.pdf") # PDF with empty page
# store PDFs the old way
pdf(file="D:/A.pdf") # OK
print(figureA)
dev.off()
pdf(file="D:/B.pdf") # hangs and when closing RStudio, writes file
print(figureB)
dev.off()
以下适合我。您需要在每个绘图后调用 ggsave
。从文档中您会看到 ggplot
使用 last_plot()
。我不在 Windows,所以我改变了路径。不管怎样:
library(ggplot2)
# plot A
figureA <- ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
ggsave(file="~/A_ggplot.pdf")
# plot B
figureB <- ggplot(data=mtcars, aes(x=wt, y=carb)) + geom_point()
ggsave(file="~/B_ggplot.pdf")
否则您需要指定它,例如与:
ggsave(file="~/B_ggplot.pdf", plot = figureB)
旧方法对我来说没有问题:
pdf(file="~/A.pdf") # OK
print(figureA)
dev.off()
pdf(file="~/B.pdf") # OK
print(figureB)
dev.off()
请提供一个sessionInfo()
。请务必更新您的软件包。请务必 运行 新 R 会话中的代码。
我在将多个 ggplot2 图存储为 pdf 时遇到一个奇怪的现象。使用 ggsave 存储时,我得到 B_ggplot.pdf OK,但是 A_ggplot.pdf 没有关闭文件(它似乎被锁定)。旧方法有时会生成空白页。似乎存在竞争条件,但我不能指责它。
谁能帮我理解一下?
这里是代码:
library(ggplot2)
# plot A
figureA <- ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
# plot B
figureB <- ggplot(data=mtcars, aes(x=wt, y=carb)) + geom_point()
# store PDFs using ggplot2
ggsave(file="D:/A_ggplot.pdf") # OK
ggsave(file="D:/B_ggplot.pdf") # PDF with empty page
# store PDFs the old way
pdf(file="D:/A.pdf") # OK
print(figureA)
dev.off()
pdf(file="D:/B.pdf") # hangs and when closing RStudio, writes file
print(figureB)
dev.off()
以下适合我。您需要在每个绘图后调用 ggsave
。从文档中您会看到 ggplot
使用 last_plot()
。我不在 Windows,所以我改变了路径。不管怎样:
library(ggplot2)
# plot A
figureA <- ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
ggsave(file="~/A_ggplot.pdf")
# plot B
figureB <- ggplot(data=mtcars, aes(x=wt, y=carb)) + geom_point()
ggsave(file="~/B_ggplot.pdf")
否则您需要指定它,例如与:
ggsave(file="~/B_ggplot.pdf", plot = figureB)
旧方法对我来说没有问题:
pdf(file="~/A.pdf") # OK
print(figureA)
dev.off()
pdf(file="~/B.pdf") # OK
print(figureB)
dev.off()
请提供一个sessionInfo()
。请务必更新您的软件包。请务必 运行 新 R 会话中的代码。