绘制多条相同图形和相同比例的曲线 [英] Plotting multiple curves same graph and same scale

查看:115
本文介绍了绘制多条相同图形和相同比例的曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想在同一张图上绘制多条曲线,但是我的新曲线要遵循由第一条曲线生成的相同y轴比例.

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.

请注意以下示例:

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轴).

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.

推荐答案

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

(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 = "")

下面的代码将更紧凑地完成任务,默认情况下,您将数字作为点,但是第二个代码为您提供典型的R型-点":

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)

这篇关于绘制多条相同图形和相同比例的曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆