图形不是从正确的x轴刻度开始

时间:2013-10-21 14:22:47

标签: r graph plot

考虑以下情节:

 x <- seq(from = 1.01, to = 1.05, by = 0.01) ## test
 pmt_test = data.frame(Amount = c(paste(0.01,  sep = " * "), paste(0.02, "r", sep = " * ")), Start = c(2,4), End = c(4,4))
 p = paste("(", pmt_test$Amount, " * (i %in% ", pmt_test$Start, ":", pmt_test$End, "))", collapse = " + ")
 res = Reduce(function(r, i) { (r + eval(parse(text = p))) * x[i] }, seq_along(x), init = 100, accumulate = TRUE)[-1]

 par(xaxs='i',yaxs='i')
 plot(res, type = "l", xaxt = 'n')
 x.labels = paste("Year", 0:4, sep=" ")
 axis(1, at = 0:4,  labels=x.labels)

enter image description here

我获得的情节从第1年开始并延伸到第4年以后。我想要的图表应该从0年级开始到4年级结束。似乎数据已经转移并且不会从x轴的正确点。

有什么方法可以解决这个问题? 谢谢!

1 个答案:

答案 0 :(得分:1)

at=1:5来电中更改axis。 R中的向量索引是从1开始的。

axis(1, at = 1:5,  labels=x.labels)