小编yfu*_*uru的帖子

scale_x_date 使情节从四月开始,而不是一月在 ggplot

我目前正在创建一个时间趋势图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 = …
Run Code Online (Sandbox Code Playgroud)

r scale ggplot2

1
推荐指数
1
解决办法
2324
查看次数

标签 统计

ggplot2 ×1

r ×1

scale ×1