R/Zoo:每年在 x 轴上显示一个勾号

发布于 2024-12-28 15:41:14 字数 353 浏览 2 评论 0原文

我有一个动物园对象,有一个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 只是改变字体大小。)

I've a zoo object, with a yearqtr index, covering about 50 years. When plotted the x-axis shows labels every 10 years, which feels a bit barren:

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

Some study got me this:

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

Which feels like swinging from one extreme to the other, as the x-axis is a blur of ticks, with ugly fractional labels. Is there an easy way to have it just show years? (What I was looking for initially was a way to say: "lower the x-axis label spacing a bit", but there seems nothing like that? cex.axis just alters the font-size.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染墨丶若流云 2025-01-04 15:41:15

当您想要使用网格而不是刻度来更好地显示数据时,这可能是其中一种(罕见)情况。正如@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() 

在此处输入图像描述

This is probably one of those (rare) situations when you want to use grid rather then ticks to better show your data. As @dirk-eddelbuettel pointed out - tweaking good axis labels is hard, especially with such density. You also might want your labels inside plot, so the grid will slightly hide their density. The easiest grid to get is with abline, unless you want to play with ggplot2, but it's uglier then standard plots in R (personal opinion). Also - make the plot wider. In fact, it's better to get rid of box around plot too ;) Below is mod of Dirk's approach:

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() 

enter image description here

梦幻的味道 2025-01-04 15:41:14

您读过help(axis)吗?

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

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

使用它来索引轴 placementlabels

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

并 sstatic.net/AapFY.png" alt="在此处输入图像描述">

我使用 las=2 和较低的 cex 值来实现此匹配。每年一次可能还是太多了。

计算“好的”轴标签确实很困难。

Did you read help(axis)?

Here is one way, just creating a simple index every four quarters:

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

and using it to index the axis placement and labels:

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

enter image description here

I used las=2 and the lower cex value to make this fit. Once every year may still be too plenty.

Computing "good" axis labels is really hard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文