在 R 中将点图保存为 pdf

发布于 2024-08-22 07:53:26 字数 823 浏览 4 评论 0原文

当在函数内执行此命令时,我无法将点图保存为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2024-08-29 07:53:26

刚刚意识到我必须将 dotplot 包装在 print() 中:

plotFunc <- function(model)    
{    
    pdf("test.pdf")    
    print(dotplot(a + b + c ~ x, data = model, type = "l", auto.key=TRUE))    
    dev.off()    
}  
plotFunc(df)

这似乎已经解决了它。

Just realized I have to wrap dotplot in print():

plotFunc <- function(model)    
{    
    pdf("test.pdf")    
    print(dotplot(a + b + c ~ x, data = model, type = "l", auto.key=TRUE))    
    dev.off()    
}  
plotFunc(df)

That seems to have solved it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文