1

I am trying to solve a similar problem that was addressed here.

I have the following linear function that I want to plot: f(t) = a*t+b. The code I use:

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)

But I still can't get the plot of the function. What am I doing wrong?

CroatiaHR
  • 615
  • 6
  • 24

2 Answers2

1

You can directly plot the linear equation without creating the dummy data.

library(ggplot2)
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
lm_eq <- function(x) 0.5 * x + 5
p + stat_function(fun = lm_eq) + xlim(0, 5)

enter image description here

Zaw
  • 1,434
  • 7
  • 15
0

Use curve().

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

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110