指定Y轴24小时范围

问题描述 投票:0回答:1

R 新手。我正在努力用小时格式指定 y 轴。我有一整年的伊斯兰祈祷时间数据,我打算做的是在图表中绘制从 1 月 24 日到 12 月 24 日之间的 5 个祈祷时间。

当我绘制图表时,y 轴不显示一天的时间范围,即从 00:01 到 23:59。它只是显示一个特定祈祷时间的范围。在此示例中,y 轴仅显示 Fajr 的范围时间。以下是代码供您参考。预先感谢。

Photo of the graph

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)

我尝试过使用 ylim:

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)

它仍然没有显示我喜欢的 y 轴范围。

r rstudio
1个回答
0
投票

这是一个基于 tidyverse 的解决方案。 由于您没有提供任何数据,我做出了(显然不正确的)假设,即祈祷时间全年不会变化。 (虽然我今天所在位置的时间是准确的...) tidyverse 最适合使用整洁的数据,因为我使用

pivot_longer
将当前(宽)格式转换为长格式。 我使用的默认值创建一个数据框,其中包含日历日期、名称(包含祈祷时间)和给出每日祈祷名称的值。

进一步定制以准确满足您的需求应该是简单的。

library(tidyverse)
library(hms)
library(lubridate)


d %>% 
  pivot_longer(
    -calendarDate
  ) %>% 
  ggplot() +
    geom_line(aes(x = calendarDate, y = value, colour = name))

给予 enter image description here

测试数据

d <- tibble(
  Fajr = parse_hms("02:59:00"),
  Sunrise = parse_hms("04:50:00"),
  Dhuhr = parse_hms("13:03:00"),
  Asr = parse_hms("17:17:00"),
  Maghrib = parse_hms("21:07:00"),
  Isha = parse_hms("22:21:00"),
  calendarDate = seq(as_date("2024-01-01"), as_date("2024-12-31"), "+1 day")
)
© www.soinside.com 2019 - 2024. All rights reserved.