我目前正在创建一个时间趋势图ggplot2。我使用scale_x_date()函数创建date_breaks6 个月,并在每 6 个月标记一次,但图表 x 轴从 4 月开始,而不是从 1 月开始。
library(lubridate)
library(stats)
library(ggplot2)
dates <- seq.Date(mdy("01-01-2013"), mdy("01-01-2017"), by = "month")
value <- rnorm(length(dates))
data <- cbind.data.frame(dates, value)
plot <- ggplot(data, aes(x = dates, y = value)) +
geom_point() +
scale_x_date(date_breaks = "6 months",
date_labels = "%b\n%Y")
Run Code Online (Sandbox Code Playgroud)
输出 x 轴从 4 月开始。
我也尝试添加expand = c(0,0):
plot <- ggplot(data, aes(x = dates, y = value)) +
geom_point() +
scale_x_date(date_breaks = "6 months",
date_labels = "%b\n%Y",
expand = c(0,0))
Run Code Online (Sandbox Code Playgroud)
请指教!
在您的代码中,日期和日期之间存在错误。那这个呢:
ggplot(data, aes(x = cdates, y = value)) +
geom_point() +
scale_x_date(breaks = seq(as.Date("2013-01-01"), as.Date("2017-01-01"), by="6 months"), date_labels = "%b\n%Y")
Run Code Online (Sandbox Code Playgroud)