本文介绍了时间序列中月份和日期的日期在ggplot2中以年份为单位进行绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在中使用 facet 多年时,我希望在时间序列图的x轴上同时具有 month day > ggplot2 .我的MWE在下面:

I want to have both month and day in the x-axis of the time series plot when using facet for years in ggplot2. My MWE is below:

set.seed(12345)
Date <- seq(as.Date("2010/1/1"), as.Date("2014/1/1"), "week")
Y <- rnorm(n=length(Date), mean=100, sd=1)
df <- data.frame(Date, Y)

df$Year <- format(df$Date, "%Y")
df$Month <- format(df$Date, "%b")
df$Day <- format(df$Date, "%d")

df$MonthDay <- format(df$Date, "%d-%b")


p <- ggplot(data=df, mapping=aes(x=MonthDay, y=Y, shape=Year, color=Year)) + geom_point() +geom_line(aes(group = 1))
p <- p + facet_grid(facets = Year ~ ., margins = FALSE) + theme_bw()
print(p)

我试图通过以下命令控制x轴标签

I tried to control the x-axis labels with the following command

p + scale_y_continuous() + scale_x_date(labels = date_format("%d-%b"))

但是它会引发以下错误消息.

But it throws the following error message.

Error: Invalid input: date_trans works with objects of class Date only

推荐答案

您非常亲密.您希望x轴可以衡量您所在年份的位置,但是您可以将x轴用作字符向量,因此也要标记每个点.如果改用连续变量表示这一点,则可能会有更好的结果.一个连续变量将是一年中的一天.

You are very close. You want the x-axis to be a measure of where in the year you are, but you have it as a character vector and so are getting every single point labelled. If you instead make a continuous variable represent this, you could have better results. One continuous variable would be the day of the year.

df$DayOfYear <- as.numeric(format(df$Date, "%j"))
ggplot(data = df,
       mapping = aes(x = DayOfYear, y = Y, shape = Year, colour = Year)) +
  geom_point() +
  geom_line() +
  facet_grid(facets = Year ~ .) +
  theme_bw()

可以使用适当的标签功能将轴格式化为更类似日期的格式,但是仍然无法以非常了解日期的方式找到中断.(最重要的是,还有一个 NA 问题.)

The axis could be formatted more date-like with an appropriate label function, but the breaks are still not being found in a very date-aware way. (And on top of that, there is an NA problem as well.)

ggplot(data = df,
       mapping = aes(x = DayOfYear, y = Y, shape = Year, colour = Year)) +
  geom_point() +
  geom_line() +
  facet_grid(facets = Year ~ .) +
  scale_x_continuous(labels = function(x) format(as.Date(as.character(x), "%j"), "%d-%b")) +
  theme_bw()

要获得良好的日期间隔,可以使用其他变量.它与原始数据在同一天,但只有一年.在这种情况下,2000年是was年.与此相关的问题主要与leap日有关,但是如果您对此不关心(非ap年的3月1日与a年的2月29日一致,等等),则可以使用:

To get the goodness of nice date breaks, a different variable can be used. One that has the same day-of-the-year as the original data, but just one year. In this case, 2000 since it was a leap year. The problems with this have mostly to do with leap days, but if you don't care about that (March 1st of a non-leap year would align with February 29th of a leap year, etc.) you can use:

df$CommonDate <- as.Date(paste0("2000-",format(df$Date, "%j")), "%Y-%j")
ggplot(data = df,
       mapping = aes(x = CommonDate, y = Y, shape = Year, colour = Year)) +
  geom_point() +
  geom_line() +
  facet_grid(facets = Year ~ .) +
  scale_x_date(labels = function(x) format(x, "%d-%b")) +
  theme_bw()

这篇关于时间序列中月份和日期的日期在ggplot2中以年份为单位进行绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 16:28