你有几个选择
使用基础 R
- 您可以使用该
plot.function
方法(调用curve
绘制函数)。如果你打电话,这就是所谓的plot(functionname)
您可能需要推出自己的功能,这样才能工作。此外,您将需要设置,ylim
以便显示这两个功能的整个范围。
# for example
fooX <- function(x) dnorm(x, mean = 0.05, sd = 0.1)
plot(fooX, from = -0.25, to = 0.35)
# I will leave the definition of fooY as an exercise.
fooY <- function(x) {# fill this is as you think fit!}
# see what it looks like
plot(fooY, from = -0.25, to = 0.35)
# now set appropriate ylim (left as an exercise)
# bonus marks if you work out a method that doesn't require this!
myYLim <- c(0, appropriateValue)
# now plot
plot(fooX, from = -0.25, to = 0.35, ylim = myYLim)
# add the second plot, (note add = TRUE)
plot(fooY, from = -0.25, to = 0.35, add = TRUE)
使用 ggplot2
ggplot
有一个函数stat_function
,它将在绘图上施加一个函数。中的示例?stat_function
显示了如何将两个具有不同均值的 Normal pdf 函数添加到同一个图中。