r - 定义函数时将范围放入变量

标签 r function range

嗨,我想要这个:

fun <- function(t) { sin(2*pi*t) - 0.5*cos(2*pi*t)}

但考虑到

a 0 < t < 10

如何绘制带有 t 限制的函数?

我的代码是

  fun <- function(t) { sin(2*pi*t) - 0.5*cos(2*pi*t)} 

  plot(fun, xlim = c(0,10))

没有xlim我该怎么做? 谢谢

最佳答案

使用fromto:

plot(fun,from=0,to=10,xlim=c(-10,20))

from-and-to

或者在ggplot2中,首先设置虚拟绘图窗口,然后将stat_functionxlimscale_x_continuous一起使用>。请注意 xlim 的工作方式与基本 R 中的工作方式有很大不同。

fun <- function(t) { sin(2*pi*t) - 0.5*cos(2*pi*t)} 
p <- ggplot(data=data.frame(x=0), mapping=aes(x=x))
p + stat_function(fun=fun, xlim=c(0,10)) + 
      scale_x_continuous(limits=c(-10,20))

enter image description here

关于r - 定义函数时将范围放入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50652278/

相关文章:

javascript - JS 中函数内变量的覆盖

python - 从列表中的各个字符串中删除重复项

jQuery UI slider 背景

将数组 reshape 为 data.frame

r - 显示包中所有内容的名称

r - 如何拆分列中的所有字符串并在所有新数据中包含前缀

r - 列 `project` 必须是一维原子向量或列表

c# - 包装函数的返回值是个好主意吗?

iterator - d2 : assigning ranges/iterators to array slices

c++ - Halide 可变域减少