R-为什么我的曲线不从(0,1)开始?

时间:2018-07-04 18:18:57

标签: r graphics

我只是想在图表上绘制三条曲线,所有曲线必须从(0,1)开始到(30,0.51)结束。问题在于曲线似乎始于(2,1)左右并终止于(31,0.51)。我的代码是:

require(graphics)
library(latex2exp)
Linear<-c(  1,0.983666667,0.967333333,0.951,0.934666667,0.918333333,0.902,0.885666667,
           0.869333333,0.853,0.836666667,0.820333333,0.804,0.787666667,0.771333333,
           0.755,0.738666667,0.722333333,0.706,0.689666667,0.673333333,0.657,0.640666667,
           0.624333333,0.608,0.591666667,0.575333333,0.559,0.542666667,0.526333333,0.51)
SqRt<-c(    1   ,   0.910538649 ,   0.873482544 ,   0.845048395 ,   0.821077298 ,
         0.799958338    ,   0.780865338 ,   0.763307513 ,   0.746965088 ,   0.731615947 ,
         0.717098368    ,   0.703290265 ,   0.690096789 ,   0.677442512 ,   0.665266275 ,
         0.653517677    ,   0.642154596 ,   0.6311414   ,   0.620447632 ,   0.610047011 ,
         0.599916675    ,   0.590036587 ,   0.580389069 ,   0.570958432 ,   0.561730676 ,
         0.552693245    ,   0.543834825 ,   0.535145184 ,   0.526615026 ,   0.518235881 ,   0.51    )
CubeRt<-c(  1   ,   0.842124513 ,   0.801135304 ,   0.772387515 ,   0.749504067 ,   0.730181506 ,
           0.71329249   ,   0.698190887 ,   0.684467813 ,   0.671846185 ,   0.660128536 ,
           0.649168592  ,   0.638854625 ,   0.629099142 ,   0.619832193 ,   0.610996874 ,
           0.602546197  ,   0.59444086  ,   0.586647616 ,   0.579138061 ,   0.571887715 ,
           0.56487531   ,   0.558082241 ,   0.551492126 ,   0.545090459 ,   0.538864322 ,
           0.532802159  ,   0.526893586 ,   0.521129232 ,   0.515500609 ,   0.51    )

g_range <- range(0, Linear, SqRt,CubeRt,na.rm = TRUE)
plot(Linear, type="l",lwd=2, col="blue", ylim=g_range, 
     axes=FALSE, ann=FALSE,lty=1)

axis(1, at=c(0,5,10,15,20,25,30))

axis(2, las=1)

box()
lines(SqRt, type="l", pch=22, lty=1,lwd=2, col="red")
lines(CubeRt, type="l", pch=24, lty=1,lwd=2, col="black")

title(xlab="mmh/RH", col.lab=rgb(0,0,0))
title(ylab="Availability", col.lab=rgb(0,0,0))

plot_colors <- c("blue","red", "black")
text <- c("Linear", "Square Root", "Cube Root")
legend(x=14, y=1.0, legend=text, fill=plot_colors, ncol=3, xpd=NA)

情节看起来像这样。

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您不同时提供xy值,则R只会开始在提供的值总数中绘制1,2,...。如果要从零开始,最好显式传递x值。例如

plot(x=0:30, y=Linear, type="l",lwd=2, col="blue", ylim=g_range, 
     axes=FALSE, ann=FALSE,lty=1)

lines(x=0:30, y=SqRt, type="l", pch=22, lty=1, lwd=2, col="red")
lines(x=0:30, y=CubeRt, type="l", pch=24, lty=1, lwd=2, col="black")