4

我有一个带有 yearqtr 索引的动物园对象,涵盖大约 50 年。绘制时,x 轴每 10 年显示一次标签,感觉有点贫瘠:

b=zoo(1:200,as.yearqtr(1900+seq(1,200)/4))
plot(b)

一些研究让我明白了:

plot(b,xaxt="n");axis(1,time(b))

感觉就像从一个极端摆动到另一个极端,因为 x 轴是一个模糊的刻度,带有丑陋的分数标签。有没有简单的方法让它只显示年份?(我最初寻找的是一种说法:“稍微降低 x 轴标签间距”,但似乎没有这样的东西?cex.axis 只是改变了字体大小。)

4

2 回答 2

4

你读了help(axis)吗?

这是一种方法,只需每四个季度创建一个简单的索引:

R> ind <- seq(1, length(b), by=4)

并使用它来索引轴的位置标签

R> plot(b,xaxt="n")
R> axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.5)

在此处输入图像描述

我使用las=2和较低的cex值来使它适合。每年一次可能还是太多了。

计算“好”轴标签真的很难。

于 2012-01-23T04:17:00.943 回答
1

当您想使用网格而不是刻度来更好地显示数据时,这可能是那些(罕见的)情况之一。正如@dirk-eddelbuettel 指出的那样——调整好的轴标签很难,尤其是在这样的密度下。您可能还希望您的标签在绘图内,因此网格会稍微隐藏它们的密度。最容易获得的网格是 with abline,除非你想玩 ggplot2,但它比 R 中的标准图更丑(个人意见)。另外 - 使情节更广泛。实际上,最好也摆脱围绕情节的框;)以下是 Dirk 方法的模型:

png("strangeplot.png",width=800)
#extend y-axis to fit inside labels and remove box
plot(b,type="n",xaxt="n",yaxt="n",ylab="",xlab="",ylim=c(min(b)-30,max(b)),bty="n"))
#use 'mpg' to get labels inside
axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.6,tick=F,mgp=c(0,-2.5,0))
axis(2,tick=F,las=1)
#you locate lines slightly to the left of label...
abline(h=seq(0,200,by=50),v=time(b)[ind]-0.5,col=gray(0.9))
#...so you need to add extra single line in the end 
abline(v=max(time(b)[ind])+0.5,col=gray(0.9))
#plot at the end to get it above grid
points(b,type="l")
dev.off() 

在此处输入图像描述

于 2012-01-23T23:29:44.360 回答