我开始使用lattice
图形包,但遇到一个问题。我希望有人能帮助我。
我想使用相应的函数绘制直方图。
这是文件foo.r
:
library("lattice")
data <- data.frame(c(1:2),c(2:3))
colnames(data) <- c("RT", "Type")
pdf("/tmp/baz.pdf")
histogram( ~ RT | factor(Type), data = data)
dev.off()
当我使用
R --vanilla < foo.r
运行此代码时,一切正常。但是,如果我使用第二个文件
bar.r
与source("bar")
并运行
R --vanilla < bar.r
,该代码会生成错误的pdf文件。现在我发现
source("bar", echo=TRUE)
解决了这个问题。这里发生了什么?这是错误还是我错过了什么?我正在将R版本2.13.1(2011-07-08)与lattice_0.19-30一起使用
最佳答案
它在FAQ for R中-您需要在调用的晶格函数周围添加print()
:
7.22 Why do lattice/trellis graphics not work?
The most likely reason is that you forgot to tell R to display the graph. Lattice functions such as xyplot() create a graph object, but do not display it (the same is true of ggplot2 graphics, and Trellis graphics in S-Plus). The print() method for the graph object produces the actual display. When you use these functions interactively at the command line, the result is automatically printed, but in source() or inside your own functions you will need an explicit print() statement.
关于如果使用source()运行,则R封装晶格将不会绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6783120/