在 R 中重复绘图命令并进行微小更改

发布于 2024-11-16 11:24:03 字数 391 浏览 2 评论 0原文

我在 R 中画了一个图,我想重复所有命令(例如 plot()legend()line())对此情节进行了一些细微的改动。例如,我想将轴设置为对数刻度并更改绘图的标题。

在 gnuplot 中,我将使用 replot 命令。

plot ...
set title "The same plot with logarithmic axes"
set logscale
replot

在 R 中可能有这样的事情吗?我想到这样做的唯一一件事(除了手动更改值并重新运行代码行)是设置一个函数,该函数要求所有可能更改的参数由用户。

谢谢你的帮助,

斯文

I made a plot in R and I want to repeat all the commands (like plot(), legend() or line()) that were carried out for this plot, with some minor changes. For example I want to set the axes to logarithmic scale and change the title of the plot.

In gnuplot I would use the replot command.

plot ...
set title "The same plot with logarithmic axes"
set logscale
replot

Is something like this possible in R. The only thing that comes to my mind of doing this (besides changing the values manually and re-run the lines of codes) would be setting up a function, that asks for all parameters that might be changed by the user.

Thanks for your help,

Sven

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

葬シ愛 2024-11-23 11:24:03

R 使用笔和纸图形模型 - 一旦在设备上绘制了图就可以了。如果要更改绘图的某些方面,则需要重播生成绘图的图形函数调用以及对代码所做的更改。

根据您真正要做的事情,有两种选择:

  1. 如果这适合您,请在了解 R 并且可以一次向 R 发送代码块的文本编辑器/IDE 中编写代码。这样代码就可以生成图形记录在一个单独的脚本中,您可以将其粘贴到/发送到 R,以便每次对脚本进行所需的更改。
  2. 如果您打算经常这样做,那么请自己编写一个包装绘图函数,该函数封装您想要的绘图代码,但允许您传入参数来更改您想要的方面。

Lattice 和 ggplot2 略有不同,因为它们基于网格图形并创建打印时在设备上生成绘图的对象。人们可以操纵该对象来改变所绘制的内容,并且使用网格可以将事物推入或弹出视口。

R uses a pen and paper graphics model - once the plot has been drawn on the device that is it. If you want to change some aspect of the plot, you need to replay the graphics function calls that produce the plot with the changes made to the code.

Depending on what you are really doing there are two options:

  1. If this is just for you, write code in a text editor / IDE that knows R and can send chunks of code at a time to R. That way the code to produce the figure is recorded in a separate script which you can paste into/send to R making the changes you need each time to the script.
  2. If you are going to be doing this often, then write yourself a wrapper plotting function that encapsulates the plot code you want but allows you to pass in arguments to alter the aspects you want.

Lattice and ggplot2 are a little different as they are based on grid graphics and create objects that when printed produce a plot on the device. One can manipulate that object to alter what is drawn, and with grid one can push and pop things on to / off a viewport.

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