【发布时间】:2014-03-02 00:18:43
【问题描述】:
我正在跟进@Gavin Simpson 对先前关于 x 轴刻度标签的问题的回复,其中包含关于非序列刻度标签的问题。这是他设置问题的响应示例:
set.seed(53)
f <- rnorm(700, 2)
dates <- seq(as.Date("04/01/2012", format = "%d/%m/%Y"),
by = "days", length = length(f))
head(f)
op <- par(mar = c(7,4,4,2) + 0.1) ## more space for the labels
plot(dates, f, xaxt = "n", ann = FALSE)
labDates <- seq(as.Date("01/01/2012", format = "%d/%m/%Y"), tail(dates, 1),
by = "months")
axis.Date(side = 1, dates, at = labDates, format = "%b %y", las = 2)
title(ylab = "f") ## draw the axis labels
title(xlab = "dates", line = 5) ## push this one down a bit in larger margin
par(op) ## reset margin
但是,我不想标记从“1 月 12 日”开始到“12 月 13 日”结束的一组有序的 x 轴刻度标签,我只想标记几个不连续且长度与 f 不同的日期,例如2012 年 3 月 3 日、2012 年 4 月 27 日、2012 年 10 月 20 日和 2013 年 5 月 8 日,而其余的情节仍然存在。所以我设置了一个日期向量来调用,但在那之后就被绊倒了:
set.seed(53)
f <- rnorm(700, 2) # the count doesn't matter
MyDates<-c("03/03/12", "04/27/13","10/20/12", "05/08/13") # non-sequential char string of dates not equal to f
plot(MyDates, f, xaxt = "n", ann = FALSE)
axis.Date(1, MyDates, at=c(1,length(MyDates), ??????)) #not complete and not right
所以我的问题是如何标记一组长度与数据不同的非连续 x 轴刻度线?
我还想在我选择的日期添加垂直线,我知道这可以通过abline() 完成,但由于我正在努力解决这个axis.Date 问题,所以还没有到达那里。
【问题讨论】:
-
响应标题,
las = 1会让y轴标签水平。 -
标题已被编辑。谢谢。
标签: r plot time-series axis-labels