题
我有一个动物园对象,有一个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 只是改变字体大小。)
解决方案
你读过吗 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
值使其适合。每年一次可能还是太多了。
计算“好的”轴标签确实很困难。
其他提示
这可能是您想要使用电网的那些(罕见)情况之一,然后勾选滴答以更好地显示您的数据。由于@ dirk-eddelbuettel指出 - 调整良好的轴标签很难,特别是具有这种密度。您也可能希望您的标签在图中,因此网格将略微隐藏它们的密度。最简单的网格,以获得abline
,除非您想玩GGPLOT2,但它是丑陋的r(个人意见)的标准地块。另外 - 让情节更宽。事实上,最好在图中摆脱盒子;)以下是Dirk方法的Mod:
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()
.
不隶属于 StackOverflow