ggplot的qplot不会在采购上执行

编程入门 行业动态 更新时间:2024-10-28 10:31:37
本文介绍了ggplot的qplot不会在采购上执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有两个源文件,第一个是 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是 gplot2 's function)

解决方案

更新:

  • .R档案 来源的选项 print.eval = TRUE 会导致评估结果的打印行为,例如在交互式命令行中。

code> source(Script.R,print.eval = TRUE)

  • .Rnw文件: knitr 默认模拟交互式命令行wrt的行为。 打印 ING。请注意,也可以将 knitr 指定为R包引擎的Sweaving引擎。

这是我的原始答案。但请注意,这种解决方法是恕我直言,现在完全过时了(它总是有利于一个小懒惰的利基)。

这是着名的 FAQ 7.22:为什么格子/网格图形不起作用?。

对于像ggplot2或lattice这样的网格图形,您需要打印图形对象才能真正绘制它。

在命令行上以交互方式自动完成。其他地方(需要源文件,循环,函数,Sweave块)你需要明确地打印它。

print(qplot (1:10,1:10))

或者,您可以重新定义 qplot 来完成打印:

qplot< - function(x,y = NULL,z = NULL,...){p < - ggplot2 :: qplot(x = x,y = y,z = z,...) print(p)}

(这会将轴标签更改为x和y)。

我在这个小插曲中使用了这种方法,我想要在交互式会话中完全按照用户编写代码来输入代码。

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)

解决方案

Update:

  • .R files: source's option print.eval=TRUE will lead to printing behaviour of the evaluation result like in the interactive command line.

source("Script.R", print.eval=TRUE)

  • .Rnw files: knitr by default emulates the behaviour of the interactive command line wrt. printing. Note that knitr 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.

print (qplot (1 : 10, 1 : 10))

Alternatively, you can redefine qplot to do the printing:

qplot <- function (x, y = NULL, z = NULL, ...) { p <- ggplot2::qplot (x = x, y = y, z = z, ...) print (p) }

(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.

更多推荐

ggplot的qplot不会在采购上执行

本文发布于:2023-10-29 10:45:18,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   采购   ggplot   qplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!