ggplot:date_breaks() 和限制之间的冲突
ggplot: conflict between date_breaks() and limits
我想每 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() )
这应该有效:
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'))
我想每 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() )
这应该有效:
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'))