如何在 R 中绘制线性函数

时间:2021-06-14 03:49:05

标签: r

我正在尝试解决已解决的类似问题 here

我有以下要绘制的线性函数:f(t) = a*t+b。 我使用的代码:

library("ggplot2")

data <- read.table(sep=",",  
                   header=T,   
                   text="a,t,b      
                   0.5, 1, 5
                   0.5, 2, 5
                   0.5, 3, 5
                   0.5, 4, 5
                   0.5, 5, 5")
eq = function(a,t,b){
  a*t+b
}
ggplot(data = data, aes(a=a, t=t, b=b)) + 
  stat_function(fun=eq)

但是我还是无法得到函数的图。我做错了什么?

2 个答案:

答案 0 :(得分:1)

您可以直接绘制线性方程,而无需创建虚拟数据。

link_directories

enter image description here

答案 1 :(得分:0)

使用curve()

curve(.5*x + 5, xlim=c(0, 5))

enter image description here

相关问题