r - 如何在ggplot R中跳过x轴上的所有其他日期标签

标签 r date ggplot2 axis

考虑下面的图(数据来自有用的链接:How to create custom date labels using ggplot with scale_x_date)

start_date <- as.Date('2020-08-08')
end_date <- as.Date('2021-05-10')

# tibble with test data
df <- tibble( dates = as.Date(as.character(seq(start_date, end_date, by = 4) ) ), 
                 yval = as.numeric(strftime(dates, format = "%d")),
                 # following two lines just to give some color to the plot
                 month = strftime(dates, format = "%m") ) %>%
                 group_by( month ) 
df
  ggplot(df, aes( x=dates, y=yval, color = month ) ) +
  geom_point() +
  scale_x_date(date_labels = "%b %d", breaks = "1 month")

enter image description here

我想要的是在 x Axis 上有第 2 个、第 4 个、第 6 个……标签。
我尝试使用“2 个月”进行休息,但它的作用相反,并删除了 1st 、 3d 、 5th …… 标签。
任何想法如何做到这一点(最好不要手动一一指定标签)

最佳答案

我试图将一个函数传递给 breaks 但这不起作用,所以我直接使用 start_dateend_date 制作了序列>:

ggplot(df, aes( x=dates, y=yval, color = month ) ) +
  geom_point() +
  scale_x_date(
    date_labels = "%b %d", 
    breaks = 
      seq(from = floor_date(start_date, "month"), to = end_date, by = "2 months"))
  )

enter image description here

关于r - 如何在ggplot R中跳过x轴上的所有其他日期标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73462183/

相关文章:

r - R中的SVM,成本值不影响测试错误率

r - 如何删除数据框R中的某些日期

python - 从unix时间字符串转换为python日期类型,当CDT存在时不转换

r - 如何填充 geom_pointrange() 后面的 ggplot 区域?

R ggplot2 : Change the spacing between the legend and the panel

r - 如何将 data.table 列发送到函数

r - 在 ggplot 外添加文本

r - 有关如何在R中运行嵌套逻辑回归的分步过程

java - 当我使用 DatePicker 在 Android 中选择新日期时,日期没有更改

r - 通过垂直分割垃圾箱,通过精确的截止点为 ggplot 直方图着色