当“add = TRUE"时,curve() 不会向我的绘图添加曲线; [英] curve() does not add curve to my plot when "add = TRUE"
问题描述
我有一个非常简单的问题,我似乎无法弄清楚.我想在一张图中绘制两个方程.我试过 add = TRUE
但这似乎不起作用.
I have a very simple problem that I can't seem to figure out. I want to plot two equations in one graph. I've tried add = TRUE
but this doesn't seem to work.
curve(0.044*x+1.638)
curve(0.3438*x+0.5155, add=TRUE)
推荐答案
嗯,实际上,curve
确实有效,但你看不到它.为了解释这一点,我们添加了几行代码:
Well, actually, the curve
does work, but you can not see it. To explain this, we add a few more lines of code:
f1 <- function(x) 0.044*x+1.638
f2 <- function(x) 0.3438*x+0.5155
x0 <- seq(0, 1, by = 0.1)
我已经通过 f1
和 f2
定义了您的示例函数.我还在 [0,1]
之间取了 11 个采样点来评估和绘制.为什么是这个范围?因为没有通过参数from
、to
(见?curve
)的指定,curve
将在这个范围内作图.
I have defined your example functions by f1
and f2
. I also take 11 sampling points between [0,1]
to evaluate and plot. Why this range? Because without specification via arguments from
, to
(see ?curve
), curve
will make plot on this range.
现在,让我们看看这个:
Now, let's see this:
>f1(x0)
[1] 1.6380 1.6424 1.6468 1.6512 1.6556 1.6600 1.6644 1.6688 1.6732 1.6776
[11] 1.6820
> f2(x0)
[1] 0.51550 0.54988 0.58426 0.61864 0.65302 0.68740 0.72178 0.75616 0.79054
[10] 0.82492 0.85930
f1
的函数值远高于 f2
.当你第一次调用curve
绘制f1
时,它会修正ylim
大致显示到f1(x)
.因此,当你添加f2(x)
时,在这个ylim
中是看不到的.
The function value of f1
is so much higher than f2
. When you call curve
for the first time to plot f1
, it will fix the ylim
to show roughly to the range of f1(x)
. Therefore, when you add f2(x)
, it will not be seen in this ylim
.
试试这个,首先选择一个共享的ylim
:
Try this instead, by first choosing a shared ylim
:
ylim <- range(c(f1(x0), f2(x0)))
curve(f1, ylim = ylim)
curve(f2, add=TRUE, col = "red")
这类问题是一个常见的陷阱.当你决定在一个图形上绘制多个对象时,你需要设置适当的xlim
和ylim
足够容纳所有对象.不要指望 R 会即时"调整这些;它不能这样做.绘制范围在绘制第一幅图时确定.
This kind of problem is a common pitfall. When you decide to plot several objects on a single graph, you need to set the appropriate xlim
and ylim
that's large enough to hold all objects. Don't expect R to adjust these "on the fly"; it can not do this. Plotting range is determined when the first plot is made.
这篇关于当“add = TRUE"时,curve() 不会向我的绘图添加曲线;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!