3

我想每 3 个月获取一次 x 轴上的日期,因此使用date_breaks("3 month"),但周期从 1/03 开始​​,希望它们去 1/1、1/4 等。这就是我尝试的方法:

datos <- data.frame(fecha= date_decimal(seq(2013.0,2013.99,by=0.01)),val=runif(100,1,2))
fini <- ymd("20130101")
ffin <- ymd("20131231")
ggplot(data=datos) + geom_point(aes(x=fecha,y=val)) +
  scale_x_datetime(breaks = date_breaks("3 month"),
                   limits=c(fini,ffin),
                   labels = date_format("%d-%m-%Y"))

也试过:

ggplot(data=datos) + geom_point(aes(x=fecha,y=val)) +
scale_x_datetime(breaks = date_breaks("3 month"),
                 labels = date_format("%d-%m-%Y")) +
xlim(c(fini,ffin))

但是然后用首字母缩略词获取月份并需要数字(看起来 xlim() 取消了之前的 scale_x_datetime() )

4

1 回答 1

3

这应该有效:

library(ggplot2)
library(scales)
library(lubridate)
datos <- data.frame(fecha= date_decimal(seq(2013.0,2013.99,by=0.01)),val=runif(100,1,2))
fini <- ymd("20130101")
ffin <- ymd("20140101")

datebreaks <- seq(as.Date(fini), as.Date(ffin), by="3 month")

ggplot(data=datos) + 
  geom_point(aes(x = as.Date(fecha), y = val)) +
  scale_x_date(breaks = datebreaks,
               limits = c(as.Date(fini), as.Date(ffin)),
               labels = date_format('%d-%m-%Y')) 
于 2015-03-23T15:03:50.460 回答