我有一个简单的XY图,显示了以小时和分钟为单位的(Y)潮位和(X)时间戳。
我想在此图上叠加一组在特定时间位置获取的数据的箱形图(它们是从n = 15个条目的栅格堆栈中生成的)。因此,我尝试按时间戳匹配箱形图的位置。
虽然在X轴上的匹配有效,但箱形图未正确显示,它们被埋在第一个图中(在第一个图中更改ylim时可见)。
有人能建议一种有效的方法来处理R基吗?只需在正确的时间让箱型图覆盖潮汐?
以下是数据链接:we.tl/t-fYwWf75IXE
这是我使用的代码:
# example for experiment 1
# aim: to match the X axis (time) boxplot position with that of the tide plot based on a POSIXct timestamp
#Tide level and timestamp file
ST1502tidelevel <- read.table("/st1502_LAT - Copy.tide", h = T, colClasses = "character")
#ratser stack from which the boxplots are derived
library(raster)
setwd("/Stacks_boxplots")
KWGSbox <- stack("KWGSboxplots.tif")
#the correct timestamp of each boxplot
Timestamp = c("201502031648",
"201502031746",
"201502031838",
"201502031947",
"201502032042",
"201502032138",
"201502032234",
"201502032341",
"201502040043",
"201502040141",
"201502040233",
"201502040333",
"201502040427",
"201502040521",
"201502040623")
#make it as time
Timestamp <- strptime(Timestamp, format = "%Y%m%d%H%M")
# p <-as.POSIXct(p, "%Y%m%d%H%M", tz ="Europe/Paris")
# df[["Timestamp"]] <- p
#convert the timestamp in the tide file to match the timestamp of the boxplots at argument
ST1502tidelevel$timestamp <- as.POSIXct(ST1502tidelevel$timestamp, "%Y%m%d%H%M", tz ="Europe/Paris")
#make a range for the tide plot - by hour
range(as.POSIXct(paste0( unique(Timestamp), " ")))
x.range <- as.POSIXct(c("2015-02-03 16:30:00 CET", "2015-02-04 06:30:00 CET"))
#positions of boxplots
x.hour <- as.POSIXct(paste0( unique(Timestamp), " "))
#plot of tide with time axis by hour
par(mar=c(4, 4.2, 2.5, 4.2))
plot(ST1502tidelevel,
xlab = "Time",
xaxt = "n",
ylab = "Tide Level",
type = "l",
lwd = 2,
ylim = c(0, 5),
xlim = x.range)
axis.POSIXct(1, at = seq(x.range[1], x.range[2], by = "hour"), labels = TRUE, las = 1, cex = 0.1)
#add boxplot time series at correct X positions
boxplot(KWGSbox,
outline=FALSE,
col = "lightgrey",
at = x.hour,
add = T)