简体   繁体   English

ggplot2:按年分面并按月对齐x轴日期

[英]ggplot2: Facetting by year and aligning x-axis dates by month

I am trying to plot daily data with days of the week (Monday:Sunday) on the y-axis, week of the year on the x-axis with monthly labels (January:December), and facet by year with each facet as its own row.我正在尝试在 y 轴上用一周中的几天(星期一:星期日)绘制每日数据,在 x 轴上用每月标签(1 月:12 月)绘制一年中的一周,并逐年绘制每个方面作为其自己的行。 I want the week of the year to align between the facets.我希望一年中的一周在各个方面保持一致。 I also want each tile to be square.我还希望每个瓷砖都是方形的。

Here is a toy dataset to work with:这是一个可以使用的玩具数据集:

my_data <- tibble(Date = seq(
  as.Date("1/11/2013", "%d/%m/%Y"), 
  as.Date("31/12/2014", "%d/%m/%Y"), 
  "days"),
  Value = runif(length(VectorofDates)))

One solution I came up with is to use lubridate::week() to number the weeks and plot by week.我想出的一种解决方案是使用lubridate::week()对周数进行编号并按周绘制。 This correctly aligns the x-axis between the facets.这正确地对齐了刻面之间的 x 轴。 The problem is, I can't figure out how to label the x-axis with monthly labels.问题是,我不知道如何用每月标签标记 x 轴。

my_data %>% 
  mutate(Week = week(Date)) %>% 
  mutate(Weekday = wday(Date, label = TRUE, week_start = 1)) %>% 
  mutate(Year = year(Date)) %>% 
ggplot(aes(fill = Value, x = Week, y = Weekday)) +
  geom_tile() +
  theme_bw() +
  facet_grid(Year ~ .) +
  coord_fixed()

按一年中的星期 Alternatively, I tried plotting by the first day of the week using lubridate::floor_date and lubridate::round_date .或者,我尝试使用lubridate::floor_datelubridate::round_date在一周的第一天绘制。 In this solution, the x-axis is correctly labeled, but the weeks don't align between the two years.在此解决方案中,x 轴已正确标记,但两年之间的周数不一致。 Also, the tiles aren't perfectly square, though I think this could be fixed by playing around with the coord_fixed ratio.此外,瓷砖不是完全正方形的,尽管我认为这可以通过使用coord_fixed比率来解决。

my_data %>% 
  mutate(Week = floor_date(Date, week_start = 1),
         Week = round_date(Week, "week", week_start = 1)) %>% 
  mutate(Weekday = wday(Date, label = TRUE, week_start = 1)) %>% 
  mutate(Year = year(Date)) %>% 
  ggplot(aes(fill = Value, x = Week, y = Weekday)) +
  geom_tile() +
  theme_bw() +
  facet_grid(Year ~ .) +
  scale_x_datetime(name = NULL, labels = label_date("%b")) +
  coord_fixed(7e5)

按星期几 Any suggestions of how to get the columns to align correctly by week of the year while labeling the months correctly?有关如何在正确标记月份的同时使列按一年中的一周正确对齐的任何建议?

The concept is a little flawed, since the same week of the year is not guaranteed to fall in the same month.这个概念有一点缺陷,因为一年中的同一周不能保证在同一个月。 However, you can get a "close enough" solution by using the labels and breaks argument of scale_x_continuous .但是,您可以通过使用scale_x_continuouslabelsbreaks参数来获得“足够接近”的解决方案。 The idea here is to write a function which takes a number of weeks, adds 7 times this value as a number of days onto an arbitrary year's 1st January, then format it as month name only using strftime :这里的想法是编写一个需要数周的函数,将该值的 7 倍作为天数添加到任意年份的 1 月 1 日,然后仅使用strftime将其格式化为月份名称:

my_data %>% 
  mutate(Week = week(Date)) %>% 
  mutate(Weekday = wday(Date, label = TRUE, week_start = 1)) %>% 
  mutate(Year = year(Date)) %>% 
ggplot(aes(fill = Value, x = Week, y = Weekday)) +
  geom_tile() +
  theme_bw() +
  facet_grid(Year ~ .) +
  coord_fixed() +
  scale_x_continuous(labels = function(x) {
    strftime(as.Date("2000-01-01") + 7 * x, "%B")
  }, breaks = seq(1, 52, 4.2))

在此处输入图像描述

Another option if you're sick of reinventing the wheel is to use the calendarHeat function in the Github-only makeR package:如果你厌倦了重新发明轮子,另一个选择是使用 Github-only makeR包中的calendarHeat函数:

install_github("jbryer/makeR")
library(makeR)

calendarHeat(my_data$Date, my_data$Value)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM