在 R 中将点图保存为 pdf
当在函数内执行此命令时,我无法将点图保存为 pdf。
正常调用时它工作正常:
df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10)
pdf("test.pdf")
dotplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)
dev.off()
但如果此代码位于函数内部,它将不起作用,只会生成一个空或空白文件:
plotFunc <- function(model)
{
pdf("test.pdf")
dotplot(a + b + c ~ x, data = model, type = "l", auto.key=TRUE)
dev.off()
}
plotFunc(df)
但是,在没有文件命令的情况下调用该函数将成功打印到图形窗口:
plotWinFunc <- function(model)
{
dotplot(a + b + c ~ x, data = model, type = "l", auto.key=TRUE)
}
plotWinFunc(df)
这导致我相信当 dotplot() 应该输出到文件时,它出了问题。文件类型并不重要,我已经尝试过 bmp 和 pdf,但两种方法都不起作用。
如何成功地将点图写入文件?我是否必须使用lattice包中的特殊命令或者我在某个地方有错误吗?
感谢您的任何帮助。
I am having trouble saving a dotplot to pdf when this command is done inside a function.
It works fine when called normally:
df <- data.frame(a = runif(10), b = runif(10), c = runif(10), x = 1:10)
pdf("test.pdf")
dotplot(a + b + c ~ x, data = df, type = "l", auto.key=TRUE)
dev.off()
But if this code is inside a function, it will not work and just makes an empty or blank file:
plotFunc <- function(model)
{
pdf("test.pdf")
dotplot(a + b + c ~ x, data = model, type = "l", auto.key=TRUE)
dev.off()
}
plotFunc(df)
However, calling the function without the file commands will successfully print to the graphics window:
plotWinFunc <- function(model)
{
dotplot(a + b + c ~ x, data = model, type = "l", auto.key=TRUE)
}
plotWinFunc(df)
This leads me to believe that something goes wrong with dotplot() when it is supposed to output to a file. And the type of file doesn't matter, I have tried with both bmp and pdf and neither method works.
How can I successfully write a dotplot to a file? Do I have to use a special command from the lattice package or do I have an error somewhere?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚意识到我必须将 dotplot 包装在 print() 中:
这似乎已经解决了它。
Just realized I have to wrap dotplot in print():
That seems to have solved it.