在x轴上绘制日期并自定义标签。

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

Plotting dates on the x-axis and customizing labels

问题

I am trying to make a graph of a time series, but I'm having trouble achieving my desired result.
我正在尝试绘制时间序列的图表,但我无法达到我期望的结果。

I have data from a time series from May 2021 to December 2022.
我有从2021年5月到2022年12月的时间序列数据。

Original data is by day, but for my visualization, months will suffice.
原始数据是按天分的,但对于我的可视化来说,月份就足够了。

I used lubridate::floor_date() and summarized the observations.
我使用了lubridate::floor_date()函数并总结了观察结果。

The resulting data is like this:
结果数据如下:

  1. n = c(30,44,58,49,52,40,31,21,29,17,25,14,20,17,10,11,7,20,5,4)) %>%
  2. dplyr::mutate(Date = lubridate::ymd(Date))```
  3. [![Plot1][1]][1]
  4. The resulting plot is alright, and I like that it omits the days by default.
  5. 结果的图表还可以,我喜欢它默认省略了日期。
  6. However, I want the x-axis tick labels to start at 2021-05 and end at 2022-12, so I looked into `scale_x_continuous` and thought I'd play around, starting from displaying 20 ticks (one for each month in my time series) with `n.breaks = 20` and go from there.
  7. 但是,我希望x轴刻度标签从2021年5月开始,结束于2022年12月,所以我查看了`scale_x_continuous`并想尝试一下,从显示20个刻度(每个月一个)开始,使用`n.breaks = 20`。
  8. The result somehow botched the labels, so I am out of ideas.
  9. 结果有些混乱,所以我想不出更多的办法。
  10. [![Plot2][2]][2]
  11. Eventually, I would like to assign custom labels using the `labels` argument inside `scale_x_continuous` to have my labels say "May 2021" or "December 2022".
  12. 最终,我想使用`scale_x_continuous`内部的`labels`参数分配自定义标签,使我的标签显示为"May 2021"或"December 2022"。
  13. Thank you all in advance!
  14. 提前谢谢大家!
  15. <details>
  16. <summary>英文:</summary>
  17. I am trying to make a graph of a time series, but I&#39;m having trouble achieving my desired result.
  18. I have data from a time series from May 2021 to December 2022. Original data is by day, but for my visualization, months will suffice. I used `lubridate::floor_date()` and summarised the observations. The resulting data is like this:

df <- data.frame(Date = c("2021-05-01","2021-06-01","2021-07-01","2021-08-01","2021-09-01","2021-10-01","2021-11-01","2021-12-01","2022-01-01","2022-02-01","2022-03-01","2022-04-01","2022-05-01","2022-06-01","2022-07-01","2022-08-01","2022-09-01","2022-10-01","2022-11-01","2022-12-01"),
n = c(30,44,58,49,52,40,31,21,29,17,25,14,20,17,10,11,7,20,5,4)) %>%
dplyr::mutate(Date = lubridate::ymd(Date))

ggplot2::ggplot(data=df,
aes(x=Date,
y=n)) +
geom_line()

  1. [![Plot1][1]][1]
  2. The resulting plot is alright, and I like that it omits the days by default. However, I want the x-axis tick labels to start at 2021-05 and end at 2022-12, so I looked into `scale_x_continuous` and thought I&#39;d play around, starting from displaying 20 ticks (one for each month in my time series) with `n.breaks = 20` and go from there. The result somehow botched the labels, so I am out of ideas.
  3. [![Plot2][2]][2]
  4. Eventually, I would like to assign custom labels using the `labels` argument inside `scale_x_continuous` to have my labels say &quot;May 2021&quot; or &quot;December 2022&quot;.
  5. Thank you all in advance!
  6. [1]: https://i.stack.imgur.com/MZjsP.png
  7. [2]: https://i.stack.imgur.com/47qQw.png
  8. </details>
  9. # 答案1
  10. **得分**: 1
  11. 一种选择是通过`breaks`参数设置所需的间断。在下面的代码中,我使用了每3个月的间隔来创建一个序列,但您可以根据喜好进行设置。此外,我使用`labels`参数将日期格式化为您所需的格式:
  12. ```R
  13. library(ggplot2)
  14. ggplot(
  15. data = df,
  16. aes(
  17. x = Date,
  18. y = n
  19. )
  20. ) +
  21. geom_line() +
  22. scale_x_continuous(
  23. breaks = seq.Date(as.Date("2021-05-01"), as.Date("2022-12-01"), by = "3 months"),
  24. labels = ~ format(.x, "%B %Y")
  25. )

编辑 没找到现成的解决方案。但一种选择是设置区域设置:

  1. lct <- Sys.getlocale("LC_TIME")
  2. lct
  3. #> [1] "de_DE.UTF-8"
  4. Sys.setlocale("LC_TIME", "fr_FR")
  5. #> [1] "fr_FR"
  6. ggplot(
  7. data = df,
  8. aes(
  9. x = Date,
  10. y = n
  11. )
  12. ) +
  13. geom_line() +
  14. scale_x_continuous(
  15. breaks = seq.Date(as.Date("2021-05-01"), as.Date("2022-12-01"), by = "3 months"),
  16. labels = ~ format.Date(.x, "%B %Y")
  17. )

然后,您可以将区域设置还原回初始值:

  1. Sys.setlocale("LC_TIME", lct)
  2. #> [1] "de_DE.UTF-8"

请注意,上述代码中使用的是R语言,不需要进行中文翻译。

英文:

One option would be to set your desired breaks via the breaks argument . In the code below I used a sequence by 3 month intervals but you could set them to your liking. Also I formatted the dates to your desired format using the labels argument:

  1. library(ggplot2)
  2. ggplot(
  3. data = df,
  4. aes(
  5. x = Date,
  6. y = n
  7. )
  8. ) +
  9. geom_line() +
  10. scale_x_continuous(
  11. breaks = seq.Date(as.Date(&quot;2021-05-01&quot;), as.Date(&quot;2022-12-01&quot;), by = &quot;3 month&quot;),
  12. labels = ~ format(.x, &quot;%B %Y&quot;)
  13. )

在x轴上绘制日期并自定义标签。

EDIT Wasn't able to find an out-of-the-box-solution. But one option would be to set the locale:

  1. lct &lt;- Sys.getlocale(&quot;LC_TIME&quot;)
  2. lct
  3. #&gt; [1] &quot;de_DE.UTF-8&quot;
  4. Sys.setlocale(&quot;LC_TIME&quot;, &quot;fr_FR&quot;)
  5. #&gt; [1] &quot;fr_FR&quot;
  6. ggplot(
  7. data = df,
  8. aes(
  9. x = Date,
  10. y = n
  11. )
  12. ) +
  13. geom_line() +
  14. scale_x_continuous(
  15. breaks = seq.Date(as.Date(&quot;2021-05-01&quot;), as.Date(&quot;2022-12-01&quot;), by = &quot;3 month&quot;),
  16. labels = ~ format.Date(.x, &quot;%B %Y&quot;)
  17. )

在x轴上绘制日期并自定义标签。<!-- -->

  1. Sys.setlocale(&quot;LC_TIME&quot;, lct)
  2. #&gt; [1] &quot;de_DE.UTF-8&quot;

huangapple
  • 本文由 发表于 2023年2月14日 20:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447583.html
匿名

发表评论

匿名网友

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

确定