Drawing sawtooth graph with lines() in R - r

I'd like to draw a graph which has a sawtooth shape, connecting some points.The problem is that when I use lines() I can't get the correct pattern
These are the points:
I'd like to get the following lines (red line):
and
With lines() I don't get the correct plot. I tried these codes:
A)
lines(opsOK$X, opsOK$VariableCost, lty=2, col=cols[2])
points(opsOK$X, opsOK$VariableCost)
B)
lines(opsOK$X, opsOK$VariableCost, type="s", lty=2, col=cols[2])
points(opsOK$X, opsOK$VariableCost)
Is there a way I can draw these graphs?
Thanks a lot!

As R. Schifini pointed out, it's necessary to add the missing points to the graph.

Related

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

How to plot a zero-order function on a scatterplot?

I would like to make a plot from a pairs of x-y values, but instead of the normal linear connection between the dots, I would like them to be connected only by horizontal and vertical lines (zero-order fit). Is this possible in R ?
You can use the option to make a step plot (I think this is what you're after?). This is done with the type="s" option to plot.
set.seed(0)
dat <- data.frame(x=sample(10), y=sample(10))
plot(dat[order(dat$x),], type="s")
points(dat, pch=16, col="steelblue")

Draw 3 curves together in the same graph and scale

I need to draw three curves together in the same graph with the same scale. I know how to draw two curves together, as the following code:
r=0.8
z=2
k=seq(0,5,by=0.1)
y1=(z^2+k*r)/(r*z+k)
y2=z*(z+k*r)/(r+k)
plot(k,y1,type='l',ylab=' ',col="red",ylim=range(c(y1,y2)))
par(new=TRUE)
plot(k,y2,type='l',col="green",ylim=range(c(y1,y2)))
It works fine, but I don't know how to add the third curve, means how to set ylim.
Any help is appreciated.
Use lines
r=0.8
z=2
k=seq(0,5,by=0.1)
y1=(z^2+k*r)/(r*z+k)
y2=z*(z+k*r)/(r+k)
y3=0.8*y2
ymin=min(c(y1,y2,y3))
ymax=max(c(y1,y2,y3))
plot(k,y1,type='l',ylab='lines',col="red",ylim=c(ymin,ymax))
par(new=TRUE)
lines(k,y2,type='l',col="green",ylim=range(c(y1,y2)))
lines(k,y3)
Here is another example, with data you have provided as a comment
r=0.8
z=3
p=seq(0.1,5,by=0.1)
y1=(p*z^2+r*z)/(p*r*z+1)
y2=z*(p+r)/(p*r+1)
y3=(p^2*z*2-1+sqrt((p^2*z*2)^2+4*p^2*r*2*z*2))/(2*p*2*r*z)
ymin=-1
ymax=max(c(y1,y2,y3))
plot(p,y1,type='l',ylab='lines',col="red",ylim=c(ymin,ymax))
par(new=TRUE)
lines(p,y2,type='l',col="green",ylim=range(c(y1,y2)))
lines(p,y3, col="blue")

How to draw a smooth curve passing through some points

I have
plot(rnorm(120), rnorm(120), col="darkblue", pch=16, xlim=c(-3,3), ylim=c(-4,4))
points(rnorm(120,-1,1), rnorm(120,2,1), col="darkred", pch=16)
points(c(-1,-1.5,-3), c(4,2,0), pch=3, cex=3)
I want to delineate a part of a graph, by drawing a smooth curve passing through a set of points.I can define 3-4 set of points but i cannot define a function. I would like to do this in R (as opposed to GIMP) as I would like to submit as SVG. What I would like to achieve is the following
Is this possible? I know this is not a sophisticated graphing question but any base R solution will do.
if I understood the question right, drawing a spline through control points should do the job:
xspline(c(-1,-1.5,-3), c(4,2,0), shape = -1)

How do I draw a straight line on plot using R?

I would like to draw a straight line on plot using the following linear equation.
y = 2.522x-1.331
I used the following code to get a scatterplot.
data=read.csv("C://book.csv")
plot(data$x,data$y)
You need to use function abline:
abline(a=-1.331, b=2.522)
Argument a is the intercept and argument b the slope.
See ?abline for more details.
Use abline, e.g.
abline(-1.331, 2.522)

Resources