gpt4 book ai didi

r - R-ggplot2时间序列x轴,显示当月的最后一天

转载 作者:行者123 更新时间:2023-12-03 09:59:33 26 4
gpt4 key购买 nike

为什么ggplot会一直给我一个月的第一天,然后再绘制一个时间序列。
这是我的代码示例:

library(ggplot2)
library(dplyr)
date <- as.Date(c("2008-01-31",
"2008-02-29",
"2008-03-31",
"2008-04-30",
"2008-05-31"))


count <- sample(5)
df <- data.frame(date = date, count = count)
df %>%
ggplot(aes(x = date, y = count))+
geom_line()+
scale_x_date(date_breaks = "1 month",
date_labels = '%m/%d')

我希望x轴显示 df或月份的最后一天的实际日期。而是显示下个月的第一天。
我尝试搜索此方法,但找不到适用的解决方案。

谢谢。

最佳答案

也许最直接的解决方案是直接使用breaks而不是date_breaks,直接引用数据框中的日期列。

df %>% 
ggplot(aes(x = date, y = count))+
geom_line()+
scale_x_date(date_labels = '%m/%d', breaks = df$date)

enter image description here

关于r - R-ggplot2时间序列x轴,显示当月的最后一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49765967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com