在函数中执行此命令时,我无法将点图保存为pdf.
I am having trouble saving a dotplot to pdf when this command is done inside a function.
正常调用时效果很好:
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)这使我相信dotplot()应该输出到文件时出了点问题.文件的类型无关紧要,我尝试使用bmp和pdf两种方法,但这两种方法均无效.
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?
感谢您的帮助.
推荐答案我刚刚意识到我必须将dotplot包装在print()中:
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)这似乎已经解决了.
更多推荐
在R中将点图保存为pdf
发布评论