R語言
axis.POSIXct
位於 graphics
包(package)。 說明
用於繪製表示日曆日期和時間的 "POSIXlt"
、 "POSIXct"
和 "Date"
類對象的函數。
用法
axis.POSIXct(side, x, at, format, labels = TRUE, ...)
axis.Date(side, x, at, format, labels = TRUE, ...)
參數
x , at |
日期時間或日期對象,或可以適當轉換的其他類型的對象。 |
side |
請參閱 |
format |
請參閱 |
labels |
指定是否要在刻度線處進行注釋的邏輯值,或者要放置在刻度點處的字符串向量。 |
... |
從其他方法傳遞或向其他方法傳遞的更多參數,通常是 graphical parameters 。 |
細節
axis.POSIXct
和 axis.Date
工作相當努力(從 R-4.3.0 通過使用 pretty
for DateTimeClasses )來選擇合適的時間單位(年、月、日、小時、分鍾或秒)和合理的輸出格式,但這可以通過提供 format
規範來覆蓋。
如果提供at
,則指定刻度和標簽的位置,而如果指定x
,則選擇合適的標簽網格。可以使用 labels = FALSE
抑製刻度標簽的打印。
"POSIXct"
輸入的 date-times 在 "tzone"
屬性給出的時區(如果有)中解釋,否則在當前時區中解釋。
date-times 的呈現方式(尤其是月份名稱)由類別 "LC_TIME"
的區域設置控製(請參閱 Sys.setlocale
)。
值
軸刻度上繪製刻度線的位置。
例子
with(beaver1, {
opar <- par(mfrow = c(2,1))
time <- strptime(paste(1990, day, time %/% 100, time %% 100),
"%Y %j %H %M")
plot(time, temp, type = "l") # axis at 5-hour intervals.
# now label every hour on the time axis
plot(time, temp, type = "l", xaxt = "n")
r <- as.POSIXct(round(range(time), "hours"))
axis.POSIXct(1, at = seq(r[1], r[2], by = "hour"), format = "%H")
par(opar) # reset changed par settings
})
plot(.leap.seconds, seq_along(.leap.seconds), type = "n", yaxt = "n",
xlab = "leap seconds", ylab = "", bty = "n")
rug(.leap.seconds)
## or as dates
lps <- as.Date(.leap.seconds)
plot(lps, seq_along(.leap.seconds),
type = "n", yaxt = "n", xlab = "leap seconds",
ylab = "", bty = "n")
rug(lps)
## 100 random dates in a 10-week period
random.dates <- as.Date("2001/1/1") + 70*sort(stats::runif(100))
plot(random.dates, 1:100)
# or for a better axis labelling
plot(random.dates, 1:100, xaxt = "n")
axis.Date(1, at = seq(as.Date("2001/1/1"), max(random.dates)+6, "weeks"))
axis.Date(1, at = seq(as.Date("2001/1/1"), max(random.dates)+6, "days"),
labels = FALSE, tcl = -0.2)
## axis.Date() with various data types:
x <- seq(as.Date("2022-01-20"), as.Date("2023-03-21"), by = "days")
plot(data.frame(x, y = 1), xaxt = "n")
legend("topleft", title = "input",
legend = c("character", "Date", "POSIXct", "POSIXlt", "numeric"),
fill = c("violet", "red", "orange", "coral1", "darkgreen"))
axis.Date(1)
axis.Date(3, at = "2022-04-01", col.axis = "violet")
axis.Date(3, at = as.Date("2022-07-01"), col.axis = "red")
axis.Date(3, at = as.POSIXct(as.Date("2022-10-01")), col.axis = "orange")
axis.Date(3, at = as.POSIXlt(as.Date("2023-01-01")), col.axis = "coral1")
axis.Date(3, at = as.integer(as.Date("2023-04-01")), col.axis = "darkgreen")
## automatically extends the format:
axis.Date(1, at = "2022-02-15", col.axis = "violet",
col = "violet", tck = -0.05, mgp = c(3,2,0))
## axis.POSIXct() with various data types (2 minutes):
x <- as.POSIXct("2022-10-01") + c(0, 60, 120)
attributes(x) # no timezone
plot(data.frame(x, y = 1), xaxt = "n")
legend("topleft", title = "input",
legend = c("character", "Date", "POSIXct", "POSIXlt", "numeric"),
fill = c("violet", "red", "orange", "coral1", "darkgreen"))
axis.POSIXct(1)
axis.POSIXct(3, at = "2022-10-01 00:01", col.axis = "violet")
axis.POSIXct(3, at = as.Date("2022-10-01"), col.axis = "red")
axis.POSIXct(3, at = as.POSIXct("2022-10-01 00:01:30"), col.axis = "orange")
axis.POSIXct(3, at = as.POSIXlt("2022-10-01 00:02"), col.axis = "coral1")
axis.POSIXct(3, at = as.numeric(as.POSIXct("2022-10-01 00:00:30")),
col.axis = "darkgreen")
## automatically extends format (here: subseconds):
axis.POSIXct(3, at = as.numeric(as.POSIXct("2022-10-01 00:00:30")) + 0.25,
col.axis = "forestgreen", col = "darkgreen", mgp = c(3,2,0))
## axis.POSIXct: 2 time zones
HST <- as.POSIXct("2022-10-01", tz = "HST") + c(0, 60, 60*60)
CET <- HST
attr(CET, "tzone") <- "CET"
plot(data.frame(HST, y = 1), xaxt = "n", xlab = "Hawaii Standard Time (HST)")
axis.POSIXct(1, HST)
axis.POSIXct(1, HST, at = "2022-10-01 00:10", col.axis = "violet")
axis.POSIXct(3, CET)
mtext(3, text = "Central European Time (CET)", line = 3)
axis.POSIXct(3, CET, at="2022-10-01 12:10", col.axis = "violet")
也可以看看
DateTimeClasses、Dates 了解課程詳情。
Axis
。
相關用法
- R axis 將軸添加到繪圖中
- R axTicks 計算軸刻度線位置
- R arrows 將箭頭添加到繪圖中
- R abline 將直線添加到繪圖中
- R assocplot 關聯圖
- R legend 將圖例添加到繪圖中
- R barplot 條形圖
- R plot.histogram 繪製直方圖
- R points 向繪圖添加點
- R stem 莖葉圖
- R mtext 將文本寫入繪圖的邊距
- R contour 顯示輪廓
- R pairs 散點圖矩陣
- R stars 星圖(蜘蛛圖/雷達圖)和線段圖
- R box 在地塊周圍畫一個方框
- R coplot 調節圖
- R smoothScatter 具有平滑密度顏色表示的散點圖
- R mosaicplot 馬賽克圖
- R bxp 從摘要中繪製箱線圖
- R plot.raster 繪製光柵圖像
- R curve 繪製函數圖
- R plot.factor 繪製因子變量
- R sunflowerplot 製作向日葵散點圖
- R plot.table 表對象的繪圖方法
- R units 圖形單位
注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Date and Date-time Plotting Functions。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。