如何为伊斯兰祷告时间绘图设置00:01-23:59的Y轴小时范围?
伊斯兰祷告时间绘图Y轴范围问题解决
问题背景
我有一整年的伊斯兰祷告时间数据,想要绘制1月24日至12月24日的Fajr、Dhuhr、Asr、Maghrib、Isha这5种祷告时间的散点图。但绘图后Y轴只显示单个祷告时间(比如Fajr)的范围,没法展示00:01至23:59的全天小时区间。我试过用ylim参数设置as.POSIXct格式的00:00-23:59范围,但问题还是没解决。
原始绘图代码
col1 <- "lightblue" col2 <- "lightgreen" col3 <- "orange" col4 <- "pink" col5 <- "turquoise" plot(data$Date, data$Fajr, xlab="Date", ylab="Time", cex=0.5, col=col1) points(data$Date, data$Fajr, pch=19, cex=0.5, col=col1) points(data$Date, data$Dhuhr, pch=19, cex=0.5, col=col2) points(data$Date, data$Asr, pch=19, cex=0.5, col=col3) points(data$Date, data$Maghrib, pch=19, cex=0.5, col=col4) points(data$Date, data$Isha, pch=19, cex=0.5, col=col5) legend("bottomright", legend=c("Fajr", "Dhuhr", "Asr", "Maghrib", "Isha"), col=c(col1, col2, col3, col4, col5), pch=19, cex=0.5)
尝试的Y轴范围设置代码
ylim_range <- as.POSIXct(c("00:00", "23:59"), format="%H:%M", tz="GMT") plot(data$Date, data$Fajr, xlab="Date", ylab="Time", cex=0.5, col=col1, ylim=ylim_range)
数据结构
structure(list(Date = structure(c(1704067200, 1704153600, 1704240000, 1704326400, 1704412800, 1704499200), tzone = "UTC", class = c("POSIXct", "POSIXt")), Fajr = structure(c(-2209054200, -2209054200, -2209054200, -2209054200, -2209054200, -2209054200), tzone = "UTC", class = c("POSIXct", "POSIXt")), Shuruq = structure(c(-2209046040, -2209046040, -2209046100, -2209046100, -2209046100, -2209046100), tzone = "UTC", class = c("POSIXct", "POSIXt")), Dhuha = structure(c(-2209043160, -2209043160, -2209043160, -2209043220, -2209043220, -2209043280), tzone = "UTC", class = c("POSIXct", "POSIXt")), Dhuhr = structure(c(-2209031580, -2209031520, -2209031520, -2209031460, -2209031460, -2209031460), tzone = "UTC", class = c("POSIXct", "POSIXt")), Asr = structure(c(-2209025640, -2209025580, -2209025520, -2209025460, -2209025400, -2209025340), tzone = "UTC", class = c("POSIXct", "POSIXt")), Maghrib = structure(c(-2209017480, -2209017360, -2209017300, -2209017240, -2209017180, -2209017120), tzone = "UTC", class = c("POSIXct", "POSIXt")), Isha = structure(c(-2209009200, -2209009140, -2209009080, -2209009020, -2209008960, -2209008900), tzone = "UTC", class = c("POSIXct", "POSIXt")), `Qibla Hour` = c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, NA_character_), Fajr_numeric = c(5.83333333333333, 5.83333333333333, 5.83333333333333, 5.83333333333333, 5.83333333333333, 5.83333333333333), Dhuhr_numeric = c(12.1166666666667, 12.1333333333333, 12.1333333333333, 12.15, 12.15, 12.15), Asr_numeric = c(13.7666666666667, 13.7833333333333, 13.8, 13.8166666666667, 13.8333333333333, 13.85 ), Maghrib_numeric = c(16.0333333333333, 16.0666666666667, 16.0833333333333, 16.1, 16.1166666666667, 16.1333333333333), Isha_numeric = c(18.3333333333333, 18.35, 18.3666666666667, 18.3833333333333, 18.4, 18.4166666666667 )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame" ))
解决方案
问题出在POSIXct类型的时间戳日期不匹配:你的祷告时间(Fajr等)是远早于Date列的历史日期(比如Fajr的时间戳对应1900年左右),而你设置的ylim是当前日期的00:00-23:59,两者不在同一时间区间,所以Y轴范围不生效。
有两种简单的解决方法:
方法1:使用数值型小时数据(推荐)
你的数据里已经有Fajr_numeric、Dhuhr_numeric等列,这些是把时间转换成了0-24的小时数值,直接用这些列绘图即可轻松设置Y轴范围:
col1 <- "lightblue" col2 <- "lightgreen" col3 <- "orange" col4 <- "pink" col5 <- "turquoise" # 初始化绘图,设置Y轴范围0-24 plot(data$Date, data$Fajr_numeric, xlab="Date", ylab="Time (Hours)", cex=0.5, col=col1, ylim=c(0,24)) # 添加各祷告时间点 points(data$Date, data$Fajr_numeric, pch=19, cex=0.5, col=col1) points(data$Date, data$Dhuhr_numeric, pch=19, cex=0.5, col=col2) points(data$Date, data$Asr_numeric, pch=19, cex=0.5, col=col3) points(data$Date, data$Maghrib_numeric, pch=19, cex=0.5, col=col4) points(data$Date, data$Isha_numeric, pch=19, cex=0.5, col=col5) # 添加图例 legend("bottomright", legend=c("Fajr", "Dhuhr", "Asr", "Maghrib", "Isha"), col=c(col1, col2, col3, col4, col5), pch=19, cex=0.5)
方法2:统一祷告时间的日期为Date列的日期
如果坚持要用POSIXct类型绘图,需要把祷告时间的日期部分替换成对应Date列的日期,只保留时间部分,然后设置Y轴范围:
library(dplyr) # 统一祷告时间的日期为对应Date的日期 data <- data %>% mutate( Fajr_fixed = as.POSIXct(paste(format(Date, "%Y-%m-%d"), format(Fajr, "%H:%M:%S")), tz="UTC"), Dhuhr_fixed = as.POSIXct(paste(format(Date, "%Y-%m-%d"), format(Dhuhr, "%H:%M:%S")), tz="UTC"), Asr_fixed = as.POSIXct(paste(format(Date, "%Y-%m-%d"), format(Asr, "%H:%M:%S")), tz="UTC"), Maghrib_fixed = as.POSIXct(paste(format(Date, "%Y-%m-%d"), format(Maghrib, "%H:%M:%S")), tz="UTC"), Isha_fixed = as.POSIXct(paste(format(Date, "%Y-%m-%d"), format(Isha, "%H:%M:%S")), tz="UTC") ) # 设置Y轴范围:取Date列的第一个日期,分别设置00:00和23:59 first_date <- format(data$Date[1], "%Y-%m-%d") ylim_range <- as.POSIXct(paste(first_date, c("00:00", "23:59")), tz="UTC") col1 <- "lightblue" col2 <- "lightgreen" col3 <- "orange" col4 <- "pink" col5 <- "turquoise" # 绘图并设置Y轴范围 plot(data$Date, data$Fajr_fixed, xlab="Date", ylab="Time", cex=0.5, col=col1, ylim=ylim_range) points(data$Date, data$Fajr_fixed, pch=19, cex=0.5, col=col1) points(data$Date, data$Dhuhr_fixed, pch=19, cex=0.5, col=col2) points(data$Date, data$Asr_fixed, pch=19, cex=0.5, col=col3) points(data$Date, data$Maghrib_fixed, pch=19, cex=0.5, col=col4) points(data$Date, data$Isha_fixed, pch=19, cex=0.5, col=col5) legend("bottomright", legend=c("Fajr", "Dhuhr", "Asr", "Maghrib", "Isha"), col=c(col1, col2, col3, col4, col5), pch=19, cex=0.5)
内容的提问来源于stack exchange,提问作者aswanism
相关产品推荐
相关产品推荐

