macos 使用ggplot2在R中保存pdf文件的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5625394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problem saving pdf file in R with ggplot2
提问by wespiserA
I am encountering an odd problem. I am able to create and save pdf file using R/ggplot2 and view them while the R Console is running. As soon as I exit the R console, Preview on Mac OS X will no longer display the PDF. I have been able to save .png files w/o problem, but for reasons beyond my control, I need to save in pdf files. The code I am using to save is as follows:
我遇到了一个奇怪的问题。我能够使用 R/ggplot2 创建和保存 pdf 文件,并在 R 控制台运行时查看它们。一旦我退出 R 控制台,Mac OS X 上的预览将不再显示 PDF。我已经能够在没有问题的情况下保存 .png 文件,但由于我无法控制的原因,我需要保存在 pdf 文件中。我用来保存的代码如下:
pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
pdf(pdfFile)
ggplot(y=count,data=allCombined, aes(x=sequenceName, fill=factor(subClass))) + geom_bar()
ggsave(pdfFile)
Has anyone encountered a similar problem? If so, what do I need to do to fix it? Thank you very much for your time.
有没有人遇到过类似的问题?如果是这样,我需要做什么来修复它?非常感谢您的宝贵时间。
回答by Gavin Simpson
The problem is that you don't close the pdf()
device with dev.off()
问题是你没有关闭pdf()
设备dev.off()
dat <- data.frame(A = 1:10, B = runif(10))
require(ggplot2)
pdf("ggplot1.pdf")
ggplot(dat, aes(x = A, y = B)) + geom_point()
dev.off()
That works, as does:
这有效,就像:
ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("ggplot1.pdf")
But don't mix the two.
但不要将两者混为一谈。
回答by Dirk Eddelbuettel
It is in the R FAQ, you need a print()
around your call to ggplot()
-- and you need to close the plotting device with dev.off()
as well, ie try
它在 R 常见问题解答中,您需要一个print()
周围的电话ggplot()
- 并且您还需要关闭绘图设备dev.off()
,即尝试
pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
pdf(pdfFile)
ggplot(y=count,data=allCombined,aes(x=sequenceName,fill=factor(subClass)))
+ geom_bar()
dev.off()
Edit:I was half-right on the dev.off()
, apparently the print()
isn;t needed. Gavin's answer has more.
编辑:我对了一半dev.off()
,显然print()
不需要。加文的回答有更多。
回答by user7732049
The following plot
下面的情节
pdf("test.pdf")
p <- qplot(hp, mpg, data=mtcars, color=am,
xlab="Horsepower", ylab="Miles per Gallon", geom="point")
p
dev.off()
works in the console but not in a function or when you source this from a file.
在控制台中工作,但在函数中或当您从文件中获取它时不起作用。
myfunc <- function() {
p <- qplot(hp, mpg, data=mtcars, color=am,
xlab="Horsepower", ylab="Miles per Gallon", geom="point")
p
}
pdf("test.pdf")
myfunc()
dev.off()
Will produce a corrupt pdf file and the way to fix it us use
会产生损坏的 pdf 文件以及我们使用的修复方法
print(p)
within a function.
一个函数内。
In a console. "p" is automatically printed but not in a function or when you source the file.
在控制台中。“p”会自动打印,但不会在函数中或在您获取文件时打印。
回答by user3482899
You can also change the filename of your pdf plot within ggsave if you want to call it something other than "ggplot1" or whatever concise object name you chose; just give the filename first and then tell it which plot you're referring to, for example:
如果您想将其命名为“ggplot1”或您选择的任何简洁的对象名称,您还可以在 ggsave 中更改 pdf 图的文件名;只需先给出文件名,然后告诉它您指的是哪个图,例如:
a <- ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("Structure.pdf",plot=a)