将数字日期转换为月份在ggplot中。

huangapple go评论98阅读模式
英文:

Convert numeric day of the year to month in ggplot

问题

I have a plot that looks like this and I need a way to convert the numbers and frequency of the x-axis to become months (i.e. Jan, Feb). The problem I have is that I probably need to keep the x values as numeric day of the year so that I can be able to display the time series for the two different years together. This is why I am doing yday(date). However, I cannot convert the day of the year back to date and then month. Notice that I am only displaying the first two months of each year.

英文:

I have a plot that looks like this and I need a way to convert the numbers and frequency of the x axis to become months (i.e. Jan, Feb). The problem I have is that I probably need to keep the x values as numeric day of the year so that I can be able to display the time series for the two different years together. This is why I am doing yday(date). However, I cannot convert the day of the year back to date and then month. Notice that I am only displaying the first two months of each years.

将数字日期转换为月份在ggplot中。

The dataset and the code to reproduce this plot are below:

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

答案1

得分: 3

以下是您要翻译的内容:

"我用于此目的的方法是创建一个用于绘图的日期,它在单一年份内(通常选择闰年以适应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)

将数字日期转换为月份在ggplot中。

英文:

The approach I've used for this is to create a date for plotting purposes that is in a single year (I usually pick a leap year to accommodate 2/29 data should it arise). You could specify that date as based on the day of the year or the month-day, which will result in slightly different behaviors past Feb 28 when dealing with non-leap years. Choose which is more appropriate for your use case, either fealty to the spacing of the date ("March 2 should always be three days after Feb 28"), or the name of the date ("May 1 should always be shown as May 1").

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

将数字日期转换为月份在ggplot中。

答案2

得分: 1

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

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

将数字日期转换为月份在ggplot中。

pmax(1,.) 是必要的,因为在这种用法中,间断点包括 0,这会被格式化为 NA

英文:

We can relabel it in scale_x_continuous:

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

将数字日期转换为月份在ggplot中。

The pmax(1,.) is needed because the breaks (on this use) includes 0, which will format as NA.

huangapple
  • 本文由 发表于 2023年5月10日 23:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定