简体   繁体   中英

R plotting graph, lines not vertical

I am trying to plot a graph on R, i have used this code to get the graph shown:

x<-seq(-2*pi,2*pi, length=100)
f<-(sin(x))+pi/4

 g<-(sin(x))*(x>=-2*pi&x<=-pi)+(sin(x))*(x>=0&x<=pi)+(-pi/4*(x>=-pi&x<=0)+(-pi/4*(x>=pi&x<=2*pi)))

plot(x,g,type="l",lty=1, col=3:1, xlim=c(-7,7), ylim=c(-1,2))
lines(x,f,lty=2, col=2)

legend(-6.8,-1,c("g(x)","f(x)"), lty=c(1,2), col=c(3,2))

图形

However I am unsure as to why the lines between -pi and 0, and pi & 2*pi, are not completely vertical. how can I change this.

Edits apprecitated sorry, not quite sure how to edit properly on here yet, thanks in advance.

I think that the lines are not vertical because your discretization of the x axis is too coarse. If in the first line defining x you set the length parameter to a higher value (say 10000), the line will appear much more vertical (although it will never be exactly vertical)

x<-seq(-2*pi,2*pi, length=10000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM