jmi*_*738 6 r time-series ggplot2
为什么 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')
Run Code Online (Sandbox Code Playgroud)
我希望 x 轴显示从df该月的最后一天或最后一天开始的实际日期。但它显示的是下个月的第一天。
我尝试搜索此问题,但找不到适用的解决方案。
谢谢。
也许最直接的解决方案是简单地使用breaks而不是date_breaks,直接引用数据框中的日期列。
df %>%
ggplot(aes(x = date, y = count))+
geom_line()+
scale_x_date(date_labels = '%m/%d', breaks = df$date)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2239 次 |
| 最近记录: |