绘制多条曲线相同的图形和相同的比例

发布于 2024-11-27 02:16:22 字数 512 浏览 0 评论 0原文

这是 这个问题

我想在同一个图表上绘制多条曲线,但这样我的新曲线就遵循第一条曲线生成的相同 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 技术交流群。

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

发布评论

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

评论(5

染柒℉ 2024-12-04 02:16:22

(典型的方法是仅使用 plot 一次来设置限制,可能包括所有系列组合的范围,然后使用 pointslines 添加单独的系列。)要多次使用 plotpar(new=TRUE),您需要确保您的第一个图具有正确的ylim 接受所有系列(在另一种情况下,您可能还需要对 xlim 使用相同的策略):

# first plot
plot(x, y1, ylim=range(c(y1,y2)))

# second plot  EDIT: needs to have same ylim
par(new = TRUE)
plot(x, y2, ylim=range(c(y1,y2)), axes = FALSE, xlab = "", ylab = "")

在此处输入图像描述

接下来的代码将更紧凑地完成任务,默认情况下,你得到的数字是点,但第二个给出的是典型的 R 类型“点”:

  matplot(x, cbind(y1,y2))
  matplot(x, cbind(y1,y2), pch=1)

(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 use points and lines to add the separate series.) To use plot multiple times with par(new=TRUE) you need to make sure that your first plot has a proper ylim to accept the all series (and in another situation, you may need to also use the same strategy for xlim):

# first plot
plot(x, y1, ylim=range(c(y1,y2)))

# second plot  EDIT: needs to have same ylim
par(new = TRUE)
plot(x, y2, ylim=range(c(y1,y2)), axes = FALSE, xlab = "", ylab = "")

enter image description here

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":

  matplot(x, cbind(y1,y2))
  matplot(x, cbind(y1,y2), pch=1)
心在旅行 2024-12-04 02:16:22

pointslines 会很方便,

  • 如果稍后生成 y2,或者
  • 新数据不具有相同的 x, 但仍然应该进入相同的坐标系。

由于您的 y 共享相同的 x,因此您还可以使用 matplot

matplot (x, cbind (y1, y2), pch = 19)

matplot (x, cbind (y1, y2), pch = 19)

(没有 pch matplopt 将绘制这y 矩阵而不是点)。

points or lines comes handy if

  • y2 is generated later, or
  • the new data does not have the same x but still should go into the same coordinate system.

As your ys share the same x, you can also use matplot:

matplot (x, cbind (y1, y2), pch = 19)

matplot (x, cbind (y1, y2), pch = 19)

(without the pch matplopt will plot the column numbers of the y matrix instead of dots).

友欢 2024-12-04 02:16:22

您在这里不太清楚您想要什么,因为考虑到您的示例代码,我认为 @DWin 在技术上是正确的。我认为您真正想要的是这样的:

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,ylim = range(c(y1,y2)))

# Add points
points(x, y2)

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:

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,ylim = range(c(y1,y2)))

# Add points
points(x, y2)

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 using points. So the above code on my machine produces this:

enter image description here

But DWin's main point about using ylim is correct.

一抹淡然 2024-12-04 02:16:22

我的解决方案是使用 ggplot2。它会自动处理这些类型的事情。最重要的是适当地排列数据。

y1 <- c(100, 200, 300, 400, 500)
y2 <- c(1, 2, 3, 4, 5)
x <- c(1, 2, 3, 4, 5)
df <- data.frame(x=rep(x,2), y=c(y1, y2), class=c(rep("y1", 5), rep("y2", 5)))

然后使用ggplot2来绘制它,

library(ggplot2)
ggplot(df, aes(x=x, y=y, color=class)) + geom_point()

这就是说将数据绘制在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.

y1 <- c(100, 200, 300, 400, 500)
y2 <- c(1, 2, 3, 4, 5)
x <- c(1, 2, 3, 4, 5)
df <- data.frame(x=rep(x,2), y=c(y1, y2), class=c(rep("y1", 5), rep("y2", 5)))

Then use ggplot2 to plot it

library(ggplot2)
ggplot(df, aes(x=x, y=y, color=class)) + geom_point()

This is saying plot the data in df, and separate the points by class.

The plot generated isenter image description here

初雪 2024-12-04 02:16:22

我不确定你想要什么,但我会使用格子。

x = rep(x,2)
y = c(y1,y2)
fac.data = as.factor(rep(1:2,each=5))
df = data.frame(x=x,y=y,z=fac.data)
# this create a data frame where I have a factor variable, z, that tells me which data I have (y1 or y2)

然后,只需绘制

xyplot(y ~x|z, df)
# or maybe
xyplot(x ~y|z, df)

I'm not sure what you want, but i'll use lattice.

x = rep(x,2)
y = c(y1,y2)
fac.data = as.factor(rep(1:2,each=5))
df = data.frame(x=x,y=y,z=fac.data)
# this create a data frame where I have a factor variable, z, that tells me which data I have (y1 or y2)

Then, just plot

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