绘制多条曲线相同的图形和相同的比例
这是 这个问题。
我想在同一个图表上绘制多条曲线,但这样我的新曲线就遵循第一条曲线生成的相同 y 轴比例。
请注意以下示例:
y1 <- c(100, 200, 300, 400, 500)
y2 <- c(1, 2, 3, 4, 5)
x <- c(1, 2, 3, 4, 5)
# first plot
plot(x, y1)
# second plot
par(new = TRUE)
plot(x, y2, axes = FALSE, xlab = "", ylab = "")
这实际上将两组值绘制在图形的相同坐标上(因为我隐藏了将使用第二个图创建的新 y 轴)。
我的问题是在绘制第二张图时如何保持相同的 y 轴比例。
This is a follow-up of this question.
I wanted to plot multiple curves on the same graph but so that my new curves respect the same y-axis scale generated by the first curve.
Notice the following example:
y1 <- c(100, 200, 300, 400, 500)
y2 <- c(1, 2, 3, 4, 5)
x <- c(1, 2, 3, 4, 5)
# first plot
plot(x, y1)
# second plot
par(new = TRUE)
plot(x, y2, axes = FALSE, xlab = "", ylab = "")
That actually plots both sets of values on the same coordinates of the graph (because I'm hiding the new y-axis that would be created with the second plot).
My question then is how to maintain the same y-axis scale when plotting the second graph.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
(典型的方法是仅使用
plot
一次来设置限制,可能包括所有系列组合的范围,然后使用points
和lines
添加单独的系列。)要多次使用plot
和par(new=TRUE)
,您需要确保您的第一个图具有正确的ylim
接受所有系列(在另一种情况下,您可能还需要对 xlim 使用相同的策略):接下来的代码将更紧凑地完成任务,默认情况下,你得到的数字是点,但第二个给出的是典型的 R 类型“点”:
(The typical method would be to use
plot
just once to set up the limits, possibly to include the range of all series combined, and then to usepoints
andlines
to add the separate series.) To useplot
multiple times withpar(new=TRUE)
you need to make sure that your first plot has a properylim
to accept the all series (and in another situation, you may need to also use the same strategy for xlim):This next code will do the task more compactly, by default you get numbers as points but the second one gives you typical R-type-"points":
则
points
或lines
会很方便,y2
,或者x
, 但仍然应该进入相同的坐标系。由于您的
y
共享相同的x
,因此您还可以使用matplot
:(没有
pch
matplopt
将绘制这y
矩阵而不是点)。points
orlines
comes handy ify2
is generated later, orx
but still should go into the same coordinate system.As your
y
s share the samex
, you can also usematplot
:(without the
pch
matplopt
will plot the column numbers of they
matrix instead of dots).您在这里不太清楚您想要什么,因为考虑到您的示例代码,我认为 @DWin 在技术上是正确的。我认为您真正想要的是这样的:
DWin 的解决方案是在隐式假设(基于您的示例代码)下运行的,即您想要在其上绘制第二组点覆盖原来的规模。这就是为什么他的图像看起来像是在 1、101 等处绘制的点。第二次调用
plot
不是您想要的,您想要添加到绘图中使用点
。因此,我的机器上的上述代码会产生以下结果:但 DWin 关于使用 ylim 的要点 是正确的。
You aren't being very clear about what you want here, since I think @DWin's is technically correct, given your example code. I think what you really want is this:
DWin's solution was operating under the implicit assumption (based on your example code) that you wanted to plot the second set of points overlayed on the original scale. That's why his image looks like the points are plotted at 1, 101, etc. Calling
plot
a second time isn't what you want, you want to add to the plot usingpoints
. So the above code on my machine produces this:But DWin's main point about using
ylim
is correct.我的解决方案是使用 ggplot2。它会自动处理这些类型的事情。最重要的是适当地排列数据。
然后使用ggplot2来绘制它,
这就是说将数据绘制在df中,并通过类来分隔点。
生成的图为
My solution is to use
ggplot2
. It takes care of these types of things automatically. The biggest thing is to arrange the data appropriately.Then use
ggplot2
to plot itThis is saying plot the data in
df
, and separate the points byclass
.The plot generated is
我不确定你想要什么,但我会使用格子。
然后,只需绘制
I'm not sure what you want, but i'll use lattice.
Then, just plot