ggplot 的 qplot 在采购时不执行
假设我有 2 个源文件,第一个名为 example1.r
,第二个名为 example2.r
(如下所示)。
example1.r
plot(1:10,1:10)
example2.r
qplot(1:10,1:10)
当我获取 example1.r 时,会绘制图表。但是,当我获取 example2.r 时,情况并非如此。这里的解决方案是什么?
(example2.r中的qplot是ggplot2的函数)
Let's assume I have 2 source files, the first one named example1.r
and the second one example2.r
(given below).
example1.r
plot(1:10,1:10)
example2.r
qplot(1:10,1:10)
When I source example1.r, the graph is drawn. It does not, however, when I source example2.r. What is the solution here?
(qplot in example2.r is ggplot2's function)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
source
的选项print.eval=TRUE
将导致打印行为评估结果就像交互式命令行中一样。knitr
默认情况下模拟交互式命令行的行为字。打印
。请注意,也可以将knitr
指定为 R 包小插图的 Sweaving 引擎。This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).
这是著名的 常见问题解答7.22: 为什么点阵/网格图形不起作用?。
对于像ggplot2或lattice这样的网格图形,您需要打印图形对象才能实际绘制它。
在命令行上交互地,这是自动完成的。在其他地方(在要获取的文件、循环、函数、Sweave 块内),您需要显式地打印它。
或者,您可以重新定义 qplot 来进行打印:(
这会将轴标签更改为 x 和 y)。
我在小插图中使用这种方法,我想编写与交互式会话中的用户键入代码完全相同的代码。
Update:
source
's optionprint.eval=TRUE
will lead to printing behaviour of the evaluation result like in the interactive command line.knitr
by default emulates the behaviour of the interactive command line wrt.print
ing. Note thatknitr
can be specified as Sweaving engine also for R package vignettes.This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).
This is the famous FAQ 7.22: Why do lattice/trellis graphics not work?.
For grid graphics like ggplot2 or lattice, you need to print the graphics object in order to actually draw it.
Interactively on the command line this is done automatically. Everywhere else (inside files to be sourced, loops, functions, Sweave chunks) you need to print it explicitly.
Alternatively, you can redefine
qplot
to do the printing:(this changes the axis labels to x and y).
I use this approach in vignettes where I want to write code exactly as a user in an interactive session would type it.