[英]How to include months in a daily plot of time series data in R
I have a plot of daily volatility in R 我有一个R的每日波动图
The dates range from "2009-04-01" to "2010-07-01", and it is kept in that format in R. When I plot it, 日期范围是从“ 2009-04-01”到“ 2010-07-01”,并以R格式保存。当我绘制它时,
plot(d, vol1, type="l") 情节(d,vol1,type =“ l”)
I only get the year 'labels' on the x axis. 我只在x轴上获得年份“标签”。 However, it would be helpful to have the month 'labels' as well. 但是,同时具有月份“标签”也会很有帮助。 Can anyone help me with this? 谁能帮我这个?
You can use tidyverse
tools to do this pretty simply, particularly scale_x_date
option date_breaks
. 您可以使用tidyverse
工具tidyverse
完成此操作,尤其是scale_x_date
选项date_breaks
。 You can also look at other answers on here or web resources if you want to do additional customisation. 如果要进行其他定制,还可以在此处或Web资源上查看其他答案。
library(tidyverse)
library(lubridate)
df = tibble(
days = seq.Date(from = ymd("2009-04-01"), to = ymd("2010-07-01"), by = "day"),
value = rnorm(457)
)
ggplot(data = df) +
geom_line(aes(x = days, y = value)) +
scale_x_date(date_breaks = "1 month", date_labels = "%m-%y")
Use the axis
command to draw it manually. 使用axis
命令手动绘制它。
# test data
d <- seq(as.Date("2009-04-01"), as.Date("2010-07-01"), "day")
v <- seq_along(d)
plot(v ~ d, xaxt = "n")
# draw X axis
months <- seq(min(d), max(d), "month")
axis(1, months, format(months, "%Y\n%b"))
giving: 赠送:
# test data
d <- seq(as.Date("2009-04-01"), as.Date("2010-07-01"), "day")
v <- seq_along(d)
plot(v ~ d, xaxt = "n")
# draw X axis
months <- seq(min(d), max(d), "month")
lab <- format(months, "%b")
lab[lab == "Jan"] <- format(months, "%Y")[lab == "Jan"]
axis(1, months, lab)
giving: 赠送:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.