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.
我想在同一个图上绘制多条曲线,但我的新曲线与第一条曲线生成的y轴刻度相同。
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).
这实际上是在图表的相同坐标上绘制两组值(因为我隐藏了将使用第二个图创建的新y轴)。
My question then is how to maintain the same y-axis scale when plotting the second graph.
那么我的问题是如何在绘制第二个图时保持相同的y轴刻度。
5 个解决方案
#1
32
(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):
(典型的方法是只使用一次绘图来设置限制,可能包括所有系列组合的范围,然后使用点和线来添加单独的系列。)使用par多次使用绘图(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 = "")
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":
下一个代码将更紧凑地执行任务,默认情况下,您将数字作为点,但第二个代码为您提供典型的R类型“点”:
matplot(x, cbind(y1,y2))
matplot(x, cbind(y1,y2), pch=1)
#2
6
points
or lines
comes handy if
点或线如果有用的话
-
y2
is generated later, or - y2稍后生成,或
- the new data does not have the same
x
but still should go into the same coordinate system. - 新数据不具有相同的x但仍应进入相同的坐标系。
As your y
s share the same x
, you can also use matplot
:
由于你的ys共享相同的x,你也可以使用matplot:
matplot (x, cbind (y1, y2), pch = 19)
(without the pch
matplopt
will plot the column numbers of the y
matrix instead of dots).
(没有pch matplopt将绘制y矩阵的列号而不是点)。
#3
5
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在技术上是正确的,给出了你的示例代码。我觉得你真正想要的是这个:
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:
DWin的解决方案是在隐式假设(基于您的示例代码)下运行,您希望绘制覆盖在原始比例上的第二组点。这就是为什么他的图像看起来像是在1,101等处绘制的。为什么第二次调用绘图不是你想要的,你想要使用点添加到绘图中。所以我机器上面的代码产生了这个:
But DWin's main point about using ylim
is correct.
但是DWin关于使用ylim的要点是正确的。
#4
3
My solution is to use ggplot2
. It takes care of these types of things automatically. The biggest thing is to arrange the data appropriately.
我的解决方案是使用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)))
Then use ggplot2
to plot it
然后使用ggplot2绘制它
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
.
这就是说用df绘制数据,并按类分开点。
The plot generated is
生成的图是
#5
1
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)
#1
32
(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):
(典型的方法是只使用一次绘图来设置限制,可能包括所有系列组合的范围,然后使用点和线来添加单独的系列。)使用par多次使用绘图(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 = "")
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":
下一个代码将更紧凑地执行任务,默认情况下,您将数字作为点,但第二个代码为您提供典型的R类型“点”:
matplot(x, cbind(y1,y2))
matplot(x, cbind(y1,y2), pch=1)
#2
6
points
or lines
comes handy if
点或线如果有用的话
-
y2
is generated later, or - y2稍后生成,或
- the new data does not have the same
x
but still should go into the same coordinate system. - 新数据不具有相同的x但仍应进入相同的坐标系。
As your y
s share the same x
, you can also use matplot
:
由于你的ys共享相同的x,你也可以使用matplot:
matplot (x, cbind (y1, y2), pch = 19)
(without the pch
matplopt
will plot the column numbers of the y
matrix instead of dots).
(没有pch matplopt将绘制y矩阵的列号而不是点)。
#3
5
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在技术上是正确的,给出了你的示例代码。我觉得你真正想要的是这个:
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:
DWin的解决方案是在隐式假设(基于您的示例代码)下运行,您希望绘制覆盖在原始比例上的第二组点。这就是为什么他的图像看起来像是在1,101等处绘制的。为什么第二次调用绘图不是你想要的,你想要使用点添加到绘图中。所以我机器上面的代码产生了这个:
But DWin's main point about using ylim
is correct.
但是DWin关于使用ylim的要点是正确的。
#4
3
My solution is to use ggplot2
. It takes care of these types of things automatically. The biggest thing is to arrange the data appropriately.
我的解决方案是使用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)))
Then use ggplot2
to plot it
然后使用ggplot2绘制它
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
.
这就是说用df绘制数据,并按类分开点。
The plot generated is
生成的图是
#5
1
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)