簡體   English   中英

R如何使用日期創建自定義x軸

[英]R how to create custom x-axis with dates

我試圖繪制一些價格與日期。 大約有10k行數據。 日期范圍為~7 / 1 / 2008~12 / 1/2011。 我希望能夠創建一個自定義的x軸, 每年都有以下刻度:

1)1/1有YYYY(即2011

2)在4/1有月份的縮寫(即Apr

3)在7/1有月份的縮寫(即Jul

4)在10/1有月份的縮寫(即Oct

這是對數據的描述:

> head(as.POSIXct(rs4p[,3]))
[1] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT"
[3] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT"
[5] "2008-06-30 20:00:00 EDT" "2008-06-30 20:00:00 EDT"
> head((rs4p[,3]))
[1] "2008-07-01" "2008-07-01" "2008-07-01" "2008-07-01" "2008-07-01"
[6] "2008-07-01"
> min((rs4p[,3]))
[1] "2008-07-01"
> max((rs4p[,3]))
[1] "2011-12-08"
> class((rs4p[,3]))
[1] "Date"

更新:

我忘記的另一件事是,當圖表完成時,我想在圖上放置一個grid() 有沒有辦法讓網格線對應年份?

您可以使用axes=FALSE來抑制默認軸,並通過調用axis.Date手動添加它們。

# Sample data
library(quantmod)
getSymbols( "^DJI" )
x <- DJI

# Plot without the date axis
matplot( 
  index(x), coredata(Ad(x)), 
  axes=FALSE, 
  xlab="", ylab="",
  type="l", lwd=3
)
axis(2, las=1)

# Some date arithmetics
all_days <- seq.Date( from=min(index(x)), to=max(index(x)), by=1 )
months  <- all_days[ format(all_days, "%d") == "01" ]
january <- all_days[ format(all_days, "%m-%d") == "01-01" ]
april   <- all_days[ format(all_days, "%m-%d") == "04-01" ]
july    <- all_days[ format(all_days, "%m-%d") == "07-01" ]
october <- all_days[ format(all_days, "%m-%d") == "10-01" ]

# Finally plot the axes
axis.Date(1, at=months,  label=FALSE, tcl=-.3) 
axis.Date(1, at=january, label=format(january, "%Y"))
axis.Date(1, at=april,   label=format(april,   "%b"))
axis.Date(1, at=july,    label=format(july,    "%b"))
axis.Date(1, at=october, label=format(october, "%b"))

您可能還想查看ggplot2:日期的默認軸很少需要調整。

文森特先得到了它,但這是我與動物園的版本

library(zoo)
my.ts <-zoo(0:1000,as.Date("2000-01-01")+0:1000)
plot(my.ts, xaxt="n")

years <-index(my.ts)[format(index(my.ts),"%m-%d") %in% "01-01"]
other.quarters <- index(my.ts)[ format(index(my.ts), "%m-%d") %in% c("04-01", "07-01","10-01")]
axis.Date(1, at=years, label=format(years,"%y"))
axis.Date(1, at=other.quarters, label=format(other.quarters, "%b"))

在此輸入圖像描述

更新:以下是添加網格線的方法:

grid(nx=NA, ny=NULL)
abline(v=c(years, other.quarters),col = "lightgray", lty = "dotted", lwd = par("lwd"))

在此輸入圖像描述

文森特打敗了我,但這就是我基本上做同樣的事情:

# Dates you want to have in the year format
fmtasY <- "Jan 01"

# Dates you want to have in the abbreviated month format (%b)
fmtasb <- c("Apr 01", "Jul 01", "Oct 01")

# Some dates
dates <- as.Date(14061:15309, origin = as.Date("1970-01-01"))

# Some intermediate labels for formatting
intermlabs <- format(dates, "%b %d")

# Set up final labels
yourlabs <- NA

# If the date is Jan 01, the label should be %Y
yourlabs[intermlabs %in% fmtasY] <- format(dates[intermlabs %in% fmtasY], "%Y")

# If the date is the first of April, July or October, label with %b
yourlabs[intermlabs %in% fmtasb] <- format(dates[intermlabs %in% fmtasb], "%b")

# Check your work
data.frame(dates, intermlabs, yourlabs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM