如何在R中将同一年的不同月份对齐在一行中,将不同年份的同一月份对齐在一列中?

fkvaft9z  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(117)

我修改了这个code来创建下面的热图。

我想重新排列图,使同一年不同月份的数据排成一行,不同年份的同一个月在同一列中对齐。我的实验是在2014–2017的四个不同年份进行的。有些年份有不同的月份,如热图所示。因此,并不是所有的月份都会正好低于对方,这很好。但是我需要将不同年份中相同的月份排列在彼此下方以进行比较。For instance、2014年5月、2015年5月、2016年5月和2017年5月应在一列中对齐。
有人可以帮我修改提供的代码来实现这种安排吗?非常感谢。
创建热图时使用的代码:

# paquetes
library(tidyverse)
library(lubridate)
library(ragg)

# color ramp
pubu <- RColorBrewer::brewer.pal(9, "PuBu")
col_p <- colorRampPalette(pubu)

theme_calendar <- function() {
  theme(
    aspect.ratio = 1 / 2,
    
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    axis.text.y = element_blank(),
    axis.text = element_text(),
    
    panel.grid = element_blank(),
    panel.background = element_blank(),
    
    strip.background = element_blank(),
    strip.text = element_text(face = "bold", size = 15),
    
    legend.position = "top",
    legend.text = element_text(hjust = .5),
    legend.title = element_text(size = 9, hjust = 1),
    
    plot.caption =  element_text(hjust = 1, size = 8),
    panel.border = element_rect(
      colour = "grey",
      fill = NA,
      size = 1
    ),
    plot.title = element_text(
      hjust = .5,
      size = 26,
      face = "bold",
      margin = margin(0, 0, 0.5, 0, unit = "cm")
    ),
    plot.subtitle = element_text(hjust = .5, size = 16)
  )
}

dat_prr <- mutate(
  dat_prr,
  week = case_when(
    month == "December" & week == 1 ~ 53,
    month == "January" &
      week %in% 52:53 ~ 0,
    TRUE ~ week
  ),
  pcat = cut(pr, c(-1, 0, 0.5, 1:5, 7, 9, 25, 81)),
  text_col = ifelse(pcat %in% c("(7,9]", "(9,25]"), "white", "black")
)

calendar_combined_rain <- ggplot(dat_prr,
                          aes(weekday, -week, fill = pcat)) +
  geom_tile(colour = "white", size = .4)  +
  geom_text(aes(label = day, colour = text_col), size = 2.5) +
  guides(fill = guide_colorsteps(
    barwidth = 25,
    barheight = .4,
    title.position = "top"
  )) +
  scale_fill_manual(
    values = c("white", col_p(13)),
    na.value = "grey90",
    drop = FALSE
  ) +
  scale_colour_manual(values = c("black", "white"), guide = FALSE) +
  facet_wrap(vars(year, month),
             scales = "free") +
  labs(title = " Daily rainfall durirng field trials (2014-2017)",
       subtitle = "Rainfall",
       fill = "mm") +
  theme_calendar()

以下是可重复的示例:

dat_prr <- structure(
  list(
    date = structure(
      c(
        16216,
        16217,
        16218,
        16219,
        16220,
        16222,
        16223,
        16230,
        16231,
        16232,
        16252,
        16253,
        16254,
        16255,
        16256,
        16283,
        16284,
        16285,
        16286,
        16287,
        16314,
        16315,
        16316,
        16317,
        16318,
        16344,
        16345,
        16346,
        16347,
        16348,
        16375,
        16376,
        16377,
        16378,
        16379,
        16574,
        16575,
        16576,
        16577,
        16578,
        16587,
        16588,
        16589,
        16590,
        16591,
        16617,
        16618,
        16619,
        16620,
        16621,
        16648,
        16649,
        16650,
        16651,
        16652,
        16679,
        16680,
        16681,
        16682,
        16683,
        16709,
        16710,
        16711,
        16712,
        16713,
        16740,
        16741,
        16742,
        16743,
        16744,
        16981,
        16982,
        16983,
        16984,
        16985,
        16986,
        16987,
        17014,
        17015,
        17016,
        17017,
        17018,
        17045,
        17046,
        17047,
        17048,
        17049,
        17075,
        17076,
        17077,
        17078,
        17079,
        17106,
        17107,
        17108,
        17109,
        17110,
        17136,
        17137,
        17138,
        17139,
        17140,
        17233,
        17234,
        17235,
        17236,
        17237,
        17257,
        17258,
        17259,
        17260,
        17261,
        17287,
        17288,
        17289,
        17290,
        17291,
        17318,
        17319,
        17320,
        17321,
        17322,
        17348,
        17349,
        17350,
        17351,
        17352,
        17379,
        17380,
        17381,
        17382,
        17383
      ),
      class = "Date"
    ),
    year = structure(
      c(
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        2L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        3L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L,
        4L
      ),
      levels = c("2014", "2015", "2016", "2017"),
      class = "factor"
    ),
    pr = c(
      0.8,
      0,
      1.4,
      3,
      0,
      0,
      0,
      3,
      0,
      2.4,
      0,
      0.599999999999994,
      0,
      0,
      0,
      36.2,
      2,
      0,
      0,
      4.59999999999999,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      13,
      0,
      0,
      7,
      0,
      0,
      0,
      0,
      0,
      0,
      1.00000001490116,
      0,
      0,
      5.20000007748604,
      3.00000007450581,
      0.200000002980232,
      0.600000008940697,
      4.6000000834465,
      0,
      0.400000005960464,
      2.40000003576279,
      0.400000005960464,
      4.60000006854534,
      0,
      0,
      0,
      0,
      2.6000000089407,
      0,
      11.9999996423721,
      0.200000002980232,
      1.60000002384186,
      0,
      0.800000011920929,
      7.60000017285347,
      38.4000002741814,
      0,
      0,
      2.40000003576279,
      23.4000004380941,
      9.40000015497208,
      3.00000004470348,
      0.200000002980232,
      0.400000005960464,
      0.200000002980232,
      6.00000014901161,
      26.3999992460012,
      0.800000011920929,
      19.999999910593,
      1.40000002086163,
      0,
      3.20000007748604,
      56.4000000655651,
      42.8000000119209,
      19.5999998599291,
      1.00000001490116,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0.200000002980232,
      0,
      0,
      0,
      0,
      0,
      10.0000001937151,
      2.80000004172325,
      0,
      0,
      2.20000003278255,
      0,
      0,
      0,
      0,
      0.400000005960464,
      24.3999996930361,
      0,
      14.9999999254942,
      0.200000002980232,
      0,
      17.8000003546476,
      21.2000000029802,
      0,
      0,
      0,
      0,
      7.79999987781048,
      0,
      0,
      0,
      15.0000003874302,
      16.4000002294779,
      0,
      0,
      0,
      0,
      0
    ),
    weekday = structure(
      c(
        1L,
        2L,
        3L,
        4L,
        5L,
        7L,
        1L,
        1L,
        2L,
        3L,
        2L,
        3L,
        4L,
        5L,
        6L,
        5L,
        6L,
        7L,
        1L,
        2L,
        1L,
        2L,
        3L,
        4L,
        5L,
        3L,
        4L,
        5L,
        6L,
        7L,
        6L,
        7L,
        1L,
        2L,
        3L,
        2L,
        3L,
        4L,
        5L,
        6L,
        1L,
        2L,
        3L,
        4L,
        5L,
        3L,
        4L,
        5L,
        6L,
        7L,
        6L,
        7L,
        1L,
        2L,
        3L,
        2L,
        3L,
        4L,
        5L,
        6L,
        4L,
        5L,
        6L,
        7L,
        1L,
        7L,
        1L,
        2L,
        3L,
        4L,
        3L,
        4L,
        5L,
        6L,
        7L,
        1L,
        2L,
        1L,
        2L,
        3L,
        4L,
        5L,
        4L,
        5L,
        6L,
        7L,
        1L,
        6L,
        7L,
        1L,
        2L,
        3L,
        2L,
        3L,
        4L,
        5L,
        6L,
        4L,
        5L,
        6L,
        7L,
        1L,
        3L,
        4L,
        5L,
        6L,
        7L,
        6L,
        7L,
        1L,
        2L,
        3L,
        1L,
        2L,
        3L,
        4L,
        5L,
        4L,
        5L,
        6L,
        7L,
        1L,
        6L,
        7L,
        1L,
        2L,
        3L,
        2L,
        3L,
        4L,
        5L,
        6L
      ),
      levels = c("Mon",
                 "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"),
      class = c("ordered",
                "factor")
    ),
    month = structure(
      c(
        5L,
        5L,
        5L,
        5L,
        5L,
        6L,
        6L,
        6L,
        6L,
        6L,
        7L,
        7L,
        7L,
        7L,
        7L,
        8L,
        8L,
        8L,
        8L,
        8L,
        9L,
        9L,
        9L,
        9L,
        9L,
        10L,
        10L,
        10L,
        10L,
        10L,
        11L,
        11L,
        11L,
        11L,
        11L,
        5L,
        5L,
        5L,
        5L,
        5L,
        6L,
        6L,
        6L,
        6L,
        6L,
        7L,
        7L,
        7L,
        7L,
        7L,
        8L,
        8L,
        8L,
        8L,
        8L,
        9L,
        9L,
        9L,
        9L,
        9L,
        10L,
        10L,
        10L,
        10L,
        10L,
        11L,
        11L,
        11L,
        11L,
        11L,
        6L,
        6L,
        7L,
        7L,
        7L,
        7L,
        7L,
        8L,
        8L,
        8L,
        8L,
        8L,
        9L,
        9L,
        9L,
        9L,
        9L,
        10L,
        10L,
        10L,
        10L,
        10L,
        11L,
        11L,
        11L,
        11L,
        11L,
        12L,
        12L,
        12L,
        12L,
        12L,
        3L,
        3L,
        3L,
        3L,
        3L,
        4L,
        4L,
        4L,
        4L,
        4L,
        5L,
        5L,
        5L,
        5L,
        5L,
        6L,
        6L,
        6L,
        6L,
        6L,
        7L,
        7L,
        7L,
        7L,
        7L,
        8L,
        8L,
        8L,
        8L,
        8L
      ),
      levels = c(
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
      ),
      class = c("ordered", "factor")
    ),
    week = c(
      22,
      22,
      22,
      22,
      22,
      22,
      23,
      24,
      24,
      24,
      27,
      27,
      27,
      27,
      27,
      31,
      31,
      31,
      32,
      32,
      36,
      36,
      36,
      36,
      36,
      40,
      40,
      40,
      40,
      40,
      44,
      44,
      45,
      45,
      45,
      21,
      21,
      21,
      21,
      21,
      23,
      23,
      23,
      23,
      23,
      27,
      27,
      27,
      27,
      27,
      31,
      31,
      32,
      32,
      32,
      36,
      36,
      36,
      36,
      36,
      40,
      40,
      40,
      40,
      41,
      44,
      45,
      45,
      45,
      45,
      26,
      26,
      26,
      26,
      26,
      27,
      27,
      31,
      31,
      31,
      31,
      31,
      35,
      35,
      35,
      35,
      36,
      39,
      39,
      40,
      40,
      40,
      44,
      44,
      44,
      44,
      44,
      48,
      48,
      48,
      48,
      49,
      10,
      10,
      10,
      10,
      10,
      13,
      13,
      14,
      14,
      14,
      18,
      18,
      18,
      18,
      18,
      22,
      22,
      22,
      22,
      23,
      26,
      26,
      27,
      27,
      27,
      31,
      31,
      31,
      31,
      31
    ),
    day = c(
      26L,
      27L,
      28L,
      29L,
      30L,
      1L,
      2L,
      9L,
      10L,
      11L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      19L,
      20L,
      21L,
      22L,
      23L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      29L,
      30L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      8L,
      9L,
      10L,
      11L,
      12L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L,
      1L,
      2L,
      3L,
      4L,
      5L
    ),
    pcat = structure(
      c(
        3L,
        1L,
        4L,
        5L,
        1L,
        1L,
        1L,
        5L,
        1L,
        5L,
        1L,
        3L,
        1L,
        1L,
        1L,
        11L,
        4L,
        1L,
        1L,
        7L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        10L,
        1L,
        1L,
        8L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        4L,
        1L,
        1L,
        8L,
        6L,
        2L,
        3L,
        7L,
        1L,
        2L,
        5L,
        2L,
        7L,
        1L,
        1L,
        1L,
        1L,
        5L,
        1L,
        10L,
        2L,
        4L,
        1L,
        3L,
        9L,
        11L,
        1L,
        1L,
        5L,
        10L,
        10L,
        6L,
        2L,
        2L,
        2L,
        8L,
        11L,
        3L,
        10L,
        4L,
        1L,
        6L,
        11L,
        11L,
        10L,
        4L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        1L,
        2L,
        1L,
        1L,
        1L,
        1L,
        1L,
        10L,
        5L,
        1L,
        1L,
        5L,
        1L,
        1L,
        1L,
        1L,
        2L,
        10L,
        1L,
        10L,
        2L,
        1L,
        10L,
        10L,
        1L,
        1L,
        1L,
        1L,
        9L,
        1L,
        1L,
        1L,
        10L,
        10L,
        1L,
        1L,
        1L,
        1L,
        1L
      ),
      levels = c(
        "(-1,0]",
        "(0,0.5]",
        "(0.5,1]",
        "(1,2]",
        "(2,3]",
        "(3,4]",
        "(4,5]",
        "(5,7]",
        "(7,9]",
        "(9,25]",
        "(25,81]"
      ),
      class = "factor"
    ),
    text_col = c(
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "black",
      "black",
      "black",
      "white",
      "white",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "white",
      "black",
      "black",
      "white",
      "white",
      "black",
      "black",
      "black",
      "black",
      "white",
      "black",
      "black",
      "black",
      "white",
      "white",
      "black",
      "black",
      "black",
      "black",
      "black"
    )
  ),
  row.names = c(NA,-132L),
  class = c("tbl_df",
            "tbl", "data.frame")
)
j2cgzkjk

j2cgzkjk1#

绘制一年中每个方面的整个周数范围会导致数据看起来太挤,所以我修改了周数,以表示每个方面的相对周数。然后facet_grid按预期工作。

calendar_combined_rain <- ggplot(dat_prr %>%
                                   mutate(week = week - min(week), 
                                          .by = c(year, month)),
                                 aes(weekday, -week, fill = pcat)) +
...
facet_grid(year~month,
         scales = "free") +
...

相关问题