英文:
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:
结果数据如下:
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))```
[![Plot1][1]][1]
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'd play around, starting from displaying 20 ticks (one for each month in my time series) with `n.breaks = 20` and go from there.
但是,我希望x轴刻度标签从2021年5月开始,结束于2022年12月,所以我查看了`scale_x_continuous`并想尝试一下,从显示20个刻度(每个月一个)开始,使用`n.breaks = 20`。
The result somehow botched the labels, so I am out of ideas.
结果有些混乱,所以我想不出更多的办法。
[![Plot2][2]][2]
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".
最终,我想使用`scale_x_continuous`内部的`labels`参数分配自定义标签,使我的标签显示为"May 2021"或"December 2022"。
Thank you all in advance!
提前谢谢大家!
<details>
<summary>英文:</summary>
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. 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()
[![Plot1][1]][1]
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'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.
[![Plot2][2]][2]
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".
Thank you all in advance!
[1]: https://i.stack.imgur.com/MZjsP.png
[2]: https://i.stack.imgur.com/47qQw.png
</details>
# 答案1
**得分**: 1
一种选择是通过`breaks`参数设置所需的间断。在下面的代码中,我使用了每3个月的间隔来创建一个序列,但您可以根据喜好进行设置。此外,我使用`labels`参数将日期格式化为您所需的格式:
```R
library(ggplot2)
ggplot(
data = df,
aes(
x = Date,
y = n
)
) +
geom_line() +
scale_x_continuous(
breaks = seq.Date(as.Date("2021-05-01"), as.Date("2022-12-01"), by = "3 months"),
labels = ~ format(.x, "%B %Y")
)
编辑 没找到现成的解决方案。但一种选择是设置区域设置:
lct <- Sys.getlocale("LC_TIME")
lct
#> [1] "de_DE.UTF-8"
Sys.setlocale("LC_TIME", "fr_FR")
#> [1] "fr_FR"
ggplot(
data = df,
aes(
x = Date,
y = n
)
) +
geom_line() +
scale_x_continuous(
breaks = seq.Date(as.Date("2021-05-01"), as.Date("2022-12-01"), by = "3 months"),
labels = ~ format.Date(.x, "%B %Y")
)
然后,您可以将区域设置还原回初始值:
Sys.setlocale("LC_TIME", lct)
#> [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:
library(ggplot2)
ggplot(
data = df,
aes(
x = Date,
y = n
)
) +
geom_line() +
scale_x_continuous(
breaks = seq.Date(as.Date("2021-05-01"), as.Date("2022-12-01"), by = "3 month"),
labels = ~ format(.x, "%B %Y")
)
EDIT Wasn't able to find an out-of-the-box-solution. But one option would be to set the locale:
lct <- Sys.getlocale("LC_TIME")
lct
#> [1] "de_DE.UTF-8"
Sys.setlocale("LC_TIME", "fr_FR")
#> [1] "fr_FR"
ggplot(
data = df,
aes(
x = Date,
y = n
)
) +
geom_line() +
scale_x_continuous(
breaks = seq.Date(as.Date("2021-05-01"), as.Date("2022-12-01"), by = "3 month"),
labels = ~ format.Date(.x, "%B %Y")
)
<!-- -->
Sys.setlocale("LC_TIME", lct)
#> [1] "de_DE.UTF-8"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论