在同一窗口中绘制一个或多个图
我想比较两条曲线,是否可以使用 R 绘制一个图,然后在其上绘制另一个图?如何 ?
谢谢。
I want compare two curves, it's possible with R to draw a plot and then draw another plot over it ? how ?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用基本 R,您可以绘制一条曲线,然后使用
lines()
参数添加第二条曲线。这是一个简单的示例:或者,如果您想使用 ggplot2,这里有两种方法 - 一种在同一个图上绘制不同的颜色,另一种为每个变量生成单独的图。这里的技巧是首先将数据“融化”为长格式。
With base R, you can plot your one curve and then add the second curve with the
lines()
argument. Here's a quick example:Alternatively, if you wanted to use ggplot2, here are two methods - one plots different colors on the same plot, and the other generates separate plots for each variable. The trick here is to "melt" the data into long format first.
使用 lattice 包:
Using lattice package:
我们已经为您提供了一些解决方案。如果您继续使用基础包,您应该熟悉以下函数:plot()、lines()、abline()、points()、polygon()、segment()、rect()、box()、 arrows(), ...看看他们的帮助文件。
您应该看到基础包中的绘图作为一个窗格,其中包含您给它的坐标。在该窗格中,您可以使用上述功能绘制一整套对象。它们允许您根据需要构建图表。不过你应该记住,除非你像 Dr. G 那样使用标准设置,否则每次调用plot()都会给你一个新的窗格。还要考虑到事物可以在其他事物之上绘制,因此请考虑用于绘制事物的顺序。
参见例如:
这给你:
There's been some solutions already for you. If you stay with the base package, you should get acquainted with the functions
plot(), lines(), abline(), points(), polygon(), segments(), rect(), box(), arrows(), ...
Take a look at their help files.You should see a plot from the base package as a pane with the coordinates you gave it. On that pane, you can draw a whole set of objects with the abovementioned functions. They allow you to construct a graph as you want. You should remember though that, unless you play with the par settings like Dr. G showed, every call to plot() gives you a new pane. Also take into account that things can be plot over other things, so think about the order you use to plot things.
See eg:
This gives you :
看看标准杆
Have a look at par
ggplot2 对于这类事情来说是一个很棒的包:
你可以得到按曲线着色的图,每条曲线都有不同的点标记,还有一个漂亮的图例,所有这些都轻松无痛,无需任何额外的工作:
ggplot2 is a great package for this sort of thing:
You get the plot coloured by curve, and with different piont marks for each curve, and a nice legend, all painlessly without any additional work:
使用 matplot 函数同时绘制多条曲线。请帮助(matplot)了解更多信息。
Draw multiple curves at the same time with the matplot function. Do help(matplot) for more.