绘图轴不符合原点

时间:2013-10-21 19:25:38

标签: r plot

考虑以下向量:

  q1 <- c(1000000.0,  908364.8,  876009.1,  847892.8,  824808.3,  805416.2,  785266.2, 770997.1,  753908.6,  744599.9,  706777.6,  674659.9,  634654.4,  601440.4, 568259.7,  535361.3,  493679.9,  465526.5,  429766.6,  395244.7,  361483.2, 332136.6, 308574.5, 285500.6, 262166.2 ,237989.0 , 210766.1,  188578.1, 166762.3 , 140399.8  ,114865.5)

这是情节:

 dev.new(width=10, height=5)
 par(xaxs='i',yaxs='i')
 plot(q1, type = "l", lty = 1, lwd = 2, col = "green", xaxt = 'n', xlim = c(0,30), bty = "l")
 x.ticks = seq(from = 0, to = 30, by = 5)
 axis(1, at = x.ticks+1,  labels=paste("Year", x.ticks, sep=" "))

enter image description here

出于某种原因,我的x轴和y轴不符合(0,0)!我尝试使用par(xaxs='i',yaxs='i')来解决这个问题,但在这种情况下它不能解决问题吗?

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:4)

如果未指定ylim,则R将使绘图区域适合数据,而不是将绘图扩展到原点。这将解决它:

plot(q1, type = "l", lty = 1, lwd = 2, col = "green", xaxt = 'n', 
     xlim = c(0,30), ylim = c(0, max(q1,  na.rm = TRUE)), bty = "l")

我写了ylim来寻找q1中的最大值。您可以将其更改为固定值。

以下是我运行的完整代码:

q1 <- c(1000000.0,  908364.8,  876009.1,  847892.8,  824808.3,  805416.2,  
        785266.2, 770997.1,  753908.6,  744599.9,  706777.6,  674659.9,  
        634654.4,  601440.4, 568259.7,  535361.3,  493679.9,  465526.5,  
        429766.6,  395244.7,  361483.2, 332136.6, 308574.5, 285500.6, 
        262166.2 ,237989.0 , 210766.1,  188578.1, 166762.3 , 140399.8  ,114865.5)

dev.new(width=10, height=5)
par(xaxs='i',yaxs='i')
plot(q1, type = "l", lty = 1, lwd = 2, col = "green", 
     xaxt = 'n', xlim = c(1,30), ylim = c(0, max(q1)), bty = "l")
x.ticks = seq(from = 0, to = 30, by = 5)
axis(1, at = x.ticks + 1,  labels=paste("Year", x.ticks, sep=" "))