考虑下图(数据来自有用的链接: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

我想要的是NOT,2,4,6,...X轴上的标签.
我try 了"2个月"的休息,但它起到了相反的作用,删除了第1,3D,5,...标签.
有什么 idea 可以做到这一点(最好不用手动逐个指定标签)

推荐答案

我试图将函数传递给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相关问答推荐

提取rame中对应r中某个变量的n个最小正值和n个最大负值的条目

使用scale_x_continuous复制ggplot 2中的离散x轴

根据固定值范围在tible中添加新行

在值和NA的行顺序中寻找中断模式

行式dppr中的变量列名

以R中的正确顺序将日期时间字符列转换为posixct

根据R中两个变量的两个条件删除带有dspirr的行

r替换lme S4对象的字符串的一部分

如何将移除事件分配给动态创建的按钮?

Ggplot2中的重复注记

如何在所有绘图中保持条件值的 colored颜色 相同?

将小数分隔符放在R中的前两位数字之后

将饼图插入条形图

`夹心::vcovCL`不等于`AER::tobit`标准错误

在带有`R`中的`ggmosaic`的马赛克图中使用图案而不是 colored颜色

R -使用矩阵reshape 列表

使用gt_summary是否有一种方法来限制每个变量集进行配对比较?

无法将条件case_when()应用于使用!!创建的新变量Mutations

在不重复主题的情况下重新排列组

把代码写成dplyr中的group_by/摘要更简洁吗?