在R图中显示为小数的日期 [英] Dates appearing as decimals in R plot

查看:259
本文介绍了在R图中显示为小数的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在沿着x轴绘制一些月份数据的数据。不幸的是,这几个月出现在小数位。任何想法?

I’m trying to plot some data with month data along the x-axis. Unfortunately the months are appearing as decimals. Any ideas?

library(zoo)  # Requires the zoo library.
theMonths <- as.yearmon(c( "Mar 2011", "Apr 2011", "May 2011", "Jun 2011", "Jul 2011", "Aug 2011", "Sep 2011", "Oct 2011", "Nov 2011", "Dec 2011", "Jan 2012", "Feb 2012"))
x <- c(1:12)
plot(theMonths,x,axes=FALSE)
axis(1,theMonths)
# Why do the dates appear on the axis as decimals?


推荐答案

如果你看看 yearmon 类对象结构化:

If you look at how the yearmon class objects are structured:

dput(theMonths)
structure(c(2011.16666666667, 2011.25, 2011.33333333333, 2011.41666666667, 
2011.5, 2011.58333333333, 2011.66666666667, 2011.75, 2011.83333333333, 
2011.91666666667, 2012, 2012.08333333333), class = "yearmon")

您可以看到它存储这些十进制值,并且调用 as.numeric 给出:

You can see that it stores these decimal values, and a call to as.numeric gives:

[1] 2011.167 2011.250 2011.333 2011.417 2011.500 2011.583 2011.667 2011.750 2011.833
[10] 2011.917 2012.000 2012.083

如果您查看 axis ,第二个参数是,这就是你所呼吁的。当您不指定标签时,必须以值作为数字。如果您还指定了标签,问题解决了:

If you look at axis, the second argument is at and that is what you have called. When you don't specify labels it must take the at values as numerics. If you also specify labels the problem is solved:

axis(1,theMonths,theMonths)

这篇关于在R图中显示为小数的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆