考虑下面的图(数据来自有用的链接: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")
我想要的是不在 x Axis 上有第 2 个、第 4 个、第 6 个……标签。
我尝试使用“2 个月”进行休息,但它的作用相反,并删除了 1st 、 3d 、 5th …… 标签。
任何想法如何做到这一点(最好不要手动一一指定标签)
最佳答案
我试图将一个函数传递给 breaks
但这不起作用,所以我直接使用 start_date
和 end_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"))
)
关于r - 如何在ggplot R中跳过x轴上的所有其他日期标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73462183/