R语言 在ggplot中将一年中的数字日期转换为月份

pkmbmrz7  于 2023-05-11  发布在  其他
关注(0)|答案(2)|浏览(190)

我有一个图,看起来像这样,我需要一种方法来转换的数字和频率的x轴成为月(即。Jan,Feb)。我遇到的问题是,我可能需要将x值保持为一年中的数字日,这样我就可以一起显示两个不同年份的时间序列。这就是为什么我在做yday(date)。但是,我不能将一年中的某一天转换回日期,然后再转换回月份。请注意,我只显示每年的前两个月。

数据集和重现该图的代码如下:

library(data.table)
library(fpp3)

fake <-
  structure(list(date = structure(c(19358, 19359, 19360, 19361, 
                                    19362, 19363, 19364, 19365, 19366, 19367, 19368, 19369, 19370, 
                                    19371, 19372, 19373, 19374, 19375, 19376, 19377, 19378, 19379, 
                                    19380, 19381, 19382, 19383, 19384, 19385, 19386, 19387, 19388, 
                                    19389, 19390, 19391, 19392, 19393, 19394, 19395, 19396, 19397, 
                                    19398, 19399, 19400, 19401, 19402, 19403, 19404, 19405, 19406, 
                                    19407, 19408, 19409, 19410, 19411, 19412, 19413, 19414, 19415, 
                                    19416, 18993, 18994, 18995, 18996, 18997, 18998, 18999, 19000, 
                                    19001, 19002, 19003, 19004, 19005, 19006, 19007, 19008, 19009, 
                                    19010, 19011, 19012, 19013, 19014, 19015, 19016, 19017, 19018, 
                                    19019, 19020, 19021, 19022, 19023, 19024, 19025, 19026, 19027, 
                                    19028, 19029, 19030, 19031, 19032, 19033, 19034, 19035, 19036, 
                                    19037, 19038, 19039, 19040, 19041, 19042, 19043, 19044, 19045, 
                                    19046, 19047, 19048, 19049, 19050, 19051, 19358, 19359, 19360, 
                                    19361, 19362, 19363, 19364, 19365, 19366, 19367, 19368, 19369, 
                                    19370, 19371, 19372, 19373, 19374, 19375, 19376, 19377, 19378, 
                                    19379, 19380, 19381, 19382, 19383, 19384, 19385, 19386, 19387, 
                                    19388, 19389, 19390, 19391, 19392, 19393, 19394, 19395, 19396, 
                                    19397, 19398, 19399, 19400, 19401, 19402, 19403, 19404, 19405, 
                                    19406, 19407, 19408, 19409, 19410, 19411, 19412, 19413, 19414, 
                                    19415, 19416, 18994, 18995, 18996, 18997, 18998, 18999, 19000, 
                                    19001, 19002, 19003, 19004, 19005, 19006, 19007, 19008, 19009, 
                                    19010, 19011, 19012, 19013, 19014, 19015, 19016, 19017, 19018, 
                                    19019, 19020, 19021, 19022, 19023, 19024, 19025, 19026, 19027, 
                                    19028, 19029, 19030, 19031, 19032, 19033, 19034, 19035, 19036, 
                                    19037, 19038, 19039, 19040, 19041, 19042, 19043, 19044, 19045, 
                                    19046, 19047, 19048, 19049, 19050, 19051, 19358, 19359, 19360, 
                                    19361, 19362, 19363, 19364, 19365, 19366, 19367, 19368, 19369, 
                                    19370, 19371, 19372, 19373, 19374, 19375, 19376, 19377, 19378, 
                                    19379, 19380, 19381, 19382, 19383, 19384, 19385, 19386, 19387, 
                                    19388, 19389, 19390, 19391, 19392, 19393, 19394, 19395, 19396, 
                                    19397, 19398, 19399, 19400, 19401, 19402, 19403, 19404, 19405, 
                                    19406, 19407, 19408, 19409, 19410, 19411, 19412, 19413, 19414, 
                                    19415, 19416), class = "Date"), group = c("foo", "foo", "foo", 
                                                                              "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", 
                                                                              "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", 
                                                                              "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", 
                                                                              "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", 
                                                                              "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", 
                                                                              "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", 
                                                                              "foo", "foo", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", "baz", 
                                                                              "baz", "baz", "baz", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", "bar", 
                                                                              "bar", "bar", "bar"), col2 = c(1537L, 1798L, 1861L, 1512L, 1772L, 
                                                                                                             1781L, NA, 1310L, 1388L, 1363L, 1094L, 2142L, 1768L, NA, 1292L, 
                                                                                                             1560L, 1220L, 1260L, 1201L, 1415L, NA, 1027L, 1285L, 1243L, 1174L, 
                                                                                                             1298L, 1352L, NA, 1103L, 1457L, 1084L, 1187L, 1371L, 1554L, NA, 
                                                                                                             1182L, 1720L, 1529L, 1598L, 1600L, 1924L, 1310L, 1418L, 1581L, 
                                                                                                             1197L, 1313L, 1581L, 1676L, 1023L, 1846L, 3415L, 1661L, 1436L, 
                                                                                                             1875L, 1915L, 1048L, 1422L, 1842L, 1514L, 1922L, 1749L, 1348L, 
                                                                                                             1317L, 1214L, 1037L, 1264L, 1226L, 1691L, 1180L, NA, NA, NA, 
                                                                                                             NA, 1233L, 1598L, 1369L, NA, NA, NA, NA, 1212L, 1474L, 1355L, 
                                                                                                             NA, NA, NA, NA, 1170L, 1552L, 1321L, NA, NA, NA, 1369L, 1535L, 
                                                                                                             1838L, 1743L, 1027L, NA, 1262L, 1525L, 1693L, 1924L, 1806L, 1531L, 
                                                                                                             1182L, 1499L, 1646L, 1529L, 2228L, 1714L, 1684L, 1325L, 1621L, 
                                                                                                             1762L, 1554L, 2165L, 1745L, 2249L, 2069L, 2224L, 1567L, 1334L, 
                                                                                                             1409L, 1766L, 2390L, 1640L, 1266L, NA, NA, 1739L, 1340L, 1808L, 
                                                                                                             1783L, 1174L, 1075L, 1094L, 1411L, 1594L, 1812L, 1911L, 1283L, 
                                                                                                             NA, 1277L, 1237L, 1516L, 1959L, 1940L, 1063L, 1266L, 1512L, 1724L, 
                                                                                                             1835L, 2218L, 1886L, 1571L, 1529L, 2001L, 2102L, 1882L, 2318L, 
                                                                                                             2232L, 1861L, 1600L, 1770L, 1716L, 1995L, 2377L, 2039L, 1833L, 
                                                                                                             1609L, 1867L, 1903L, 1917L, 2295L, 1705L, 1802L, 1111L, 1825L, 
                                                                                                             1476L, 1586L, 1922L, 1961L, 1008L, 1334L, 1655L, 1472L, 1712L, 
                                                                                                             1638L, 1787L, 1130L, 1197L, 1474L, 1516L, 1712L, 1678L, 1838L, 
                                                                                                             1109L, 1208L, 1663L, 1667L, 1659L, 1556L, 1968L, 1199L, 1308L, 
                                                                                                             1724L, 1617L, 1514L, 1953L, 2006L, 1273L, 1344L, 2090L, 2165L, 
                                                                                                             2121L, 2121L, 2266L, 1279L, 1516L, 1993L, 2125L, 2348L, 2759L, 
                                                                                                             2400L, 1229L, 1394L, 2176L, 1852L, 1987L, 2136L, 2321L, 1497L, 
                                                                                                             1735L, 1964L, 1598L, 2134L, 2262L, 2583L, 2873L, 2953L, 1621L, 
                                                                                                             1646L, 2243L, 2514L, 2678L, 2610L, 2869L, 1714L, 1951L, 2455L, 
                                                                                                             2297L, 2682L, 2657L, 2900L, 1796L, 1842L, 2426L, 2289L, 2783L, 
                                                                                                             3251L, 4001L, 3268L, 2938L, 3845L, 3587L, 3654L, 3478L, 3320L, 
                                                                                                             1884L, 2159L, 2757L, 2774L, 2942L, 3245L, 3093L, 1520L, 1701L, 
                                                                                                             2722L, 2331L, 2617L, 3654L, 2915L, 1602L, 1779L, 2617L, 2512L, 
                                                                                                             2667L, 2772L, 2810L, 1766L, 1928L, 2720L, 2724L), value = c(75L, 
                                                                                                                                                                         99L, 97L, 25L, 93L, 44L, 99L, 53L, 87L, 30L, 47L, 6L, 88L, 19L, 
                                                                                                                                                                         39L, 16L, 87L, 62L, 27L, 37L, 71L, 42L, 82L, 62L, 53L, 9L, 59L, 
                                                                                                                                                                         98L, 15L, 20L, 30L, 69L, 94L, 88L, 99L, 100L, 28L, 25L, 62L, 
                                                                                                                                                                         34L, 75L, 57L, 49L, 43L, 52L, 50L, 18L, 59L, 16L, 42L, 91L, 61L, 
                                                                                                                                                                         75L, 2L, 63L, 41L, 63L, 88L, 96L, 75L, 81L, 65L, 93L, 57L, 41L, 
                                                                                                                                                                         34L, 59L, 22L, 69L, 72L, 36L, 61L, 23L, 40L, 70L, 44L, 70L, 87L, 
                                                                                                                                                                         77L, 23L, 83L, 15L, 68L, 66L, 53L, 17L, 24L, 33L, 22L, 17L, 9L, 
                                                                                                                                                                         5L, 55L, 82L, 23L, 37L, 41L, 39L, 32L, 29L, 7L, 82L, 94L, 93L, 
                                                                                                                                                                         39L, 60L, 36L, 54L, 56L, 7L, 44L, 10L, 45L, 9L, 36L, 71L, 86L, 
                                                                                                                                                                         79L, 1L, 94L, 20L, 6L, 60L, 46L, 64L, 48L, 7L, 95L, 33L, 53L, 
                                                                                                                                                                         62L, 19L, 33L, 91L, 95L, 86L, 29L, 15L, 28L, 87L, 4L, 49L, 93L, 
                                                                                                                                                                         90L, 2L, 53L, 27L, 78L, 10L, 86L, 2L, 90L, 4L, 85L, 77L, 20L, 
                                                                                                                                                                         53L, 29L, 68L, 99L, 24L, 5L, 74L, 17L, 54L, 98L, 64L, 69L, 1L, 
                                                                                                                                                                         80L, 74L, 38L, 71L, 4L, 12L, 98L, 3L, 15L, 100L, 22L, 79L, 43L, 
                                                                                                                                                                         89L, 28L, 56L, 72L, 26L, 36L, 54L, 85L, 92L, 56L, 60L, 71L, 9L, 
                                                                                                                                                                         76L, 36L, 87L, 87L, 61L, 11L, 87L, 65L, 54L, 25L, 80L, 14L, 17L, 
                                                                                                                                                                         17L, 100L, 33L, 24L, 3L, 92L, 100L, 64L, 98L, 43L, 56L, 89L, 
                                                                                                                                                                         47L, 21L, 10L, 78L, 4L, 49L, 90L, 81L, 97L, 15L, 94L, 70L, 2L, 
                                                                                                                                                                         31L, 99L, 71L, 70L, 64L, 55L, 91L, 3L, 81L, 55L, 22L, 60L, 32L, 
                                                                                                                                                                         38L, 27L, 66L, 16L, 38L, 28L, 7L, 77L, 55L, 15L, 41L, 82L, 62L, 
                                                                                                                                                                         71L, 32L, 19L, 29L, 61L, 28L, 9L, 93L, 95L, 34L, 67L, 62L, 45L, 
                                                                                                                                                                         50L, 18L, 29L, 40L, 13L, 72L, 72L, 51L, 54L, 12L, 1L, 19L, 87L, 
                                                                                                                                                                         47L, 58L, 11L, 96L, 8L, 50L, 80L, 71L, 79L)), class = c("tbl_ts", 
                                                                                                                                                                                                                                 "tbl_df", "tbl", "data.frame"), row.names = c(NA, -294L), key = structure(list(
                                                                                                                                                                                                                                   group = c("bar", "baz", "foo"), .rows = structure(list(178:294, 
                                                                                                                                                                                                                                                                                          60:177, 1:59), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                                                       "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                       ), row.names = c(NA, -3L), .drop = TRUE), index = structure("date", ordered = TRUE), index2 = "date", interval = structure(list(
                                                                                                                                                                                                                                                                                                                                         year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0, 
                                                                                                                                                                                                                                                                                                                                         minute = 0, second = 0, millisecond = 0, microsecond = 0, 
                                                                                                                                                                                                                                                                                                                                         nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval", 
                                                                                                                                                                                                                                                                                                                                                                                               "vctrs_rcrd", "vctrs_vctr")))
fake |>
  ggplot(aes(x = yday(date), y = value, colour = factor(year(date)))) + 
  geom_line() +
  facet_wrap(~group, nrow = 3)
iezvtpos

iezvtpos1#

我使用的方法是创建一个日期,用于绘制一个单一的年份(我通常选择一个闰年来容纳2/29数据)。您可以根据一年中的某一天或某个月的某一天来指定该日期,这将导致在处理非闰年时在2月28日之后的行为略有不同。选择哪一个更适合您的用例,要么忠于日期的间隔(“3月2日应始终是2月28日之后的三天”),要么日期的名称(“5月1日应始终显示为5月1日”)。

fake |>
  ggplot(aes(x = as.Date("2019-12-31") + yday(date), 
             # x = ymd(paste(2020, month(date), day(date))),  # alternative
             y = value, colour = factor(year(date)))) + 
  geom_line() +
  scale_x_date(date_breaks = "1 month", date_labels = "%b") +
  facet_wrap(~group, nrow = 3)

flseospp

flseospp2#

我们可以在scale_x_continuous中重新标记它:

fake |>
  ggplot(aes(x = yday(date), y = value, colour = factor(year(date)))) + 
  geom_line() +
  facet_wrap(~group, nrow = 3) +
  scale_x_continuous(
    labels = ~ format(as.Date(paste("2022", pmax(., 1)), format="%Y %j"), format = "%b-%d")
  )

需要pmax(1,.),因为break(在此使用中)包括0,它将格式化为NA

相关问题