Reputation:
I am supposed to plot the following function:
h <- function(x) 0.08-0.06*(1-exp((-x)/(1.5)))/((x)/(1.5))-0.3*((1-exp((-x)/(1.5)))/((x)/(1.5))-exp(-((x)/(1.5)))+0.6*((1-exp((-x)/(0.5)))/((x)/(0.5))-exp(-((x)/(0.5)))))
So I did:
plot(-1:24, h(-1:24), type="l")
and it would be for x-axes. But I do not know how to change it for y.
For x I want to draw it from -1 to 24 and for y from -0.5 to 0.2.
Can anybody help?
Upvotes: 0
Views: 28
Reputation: 45027
An issue with your function is that it is undefined at 0, so you get a single point at x == -1
followed by a gap until x == 1
. You'll get a better plot with more points, e.g.
x <- seq(-1, 24, len=200)
plot(x, h(x), type = "l")
(which never evaluates it at exactly zero, so the gap doesn't appear).
If you still want to change the axis limits, use
x <- seq(-1, 24, len=200)
plot(x, h(x), type = "l", ylim = c(-0.5, 2))
Upvotes: 1
Reputation: 4456
With ggplot2 you can do:
library(ggplot2)
data = data.frame(x = -1:24)
ggplot(data, aes(x=x)) + stat_function(fun=h) + ylim(-0.5,2)
Or
data = data.frame(x = -1:24, y = h(-1:24))
ggplot(data, aes(x=x, y=y)) + geom_line() + ylim(-0.5,2)
But a solution with plot
is probably easier as its a very simple thing, ggplot is kinda overkill i guess.
Upvotes: 0