ggplott2中Y轴上的格式化时间

时间:2018-10-15 03:58:36

标签: r ggplot2 time-series

我有以下代码:

file = "http://dd.weather.gc.ca/hydrometric/csv/SK/hourly/SK_hourly_hydrometric.csv"
skdat <- read.csv(file, head=T, sep=",", dec=".", stringsAsFactors = F)
colnames(skdat) <- c("ID", "Date", "WaterLevel", "Grade1", "Symbol1", 
                     "QAQC-1", "DischargeDebit", "Grade2", "Symbol2", 
                     "QAQC-2")

subds <- subset(skdat, ID=='05AH050')
subds$datetime1 <- as.numeric(as.POSIXct(subds$Date))
class(data$datetime1)


subds[1:10, ]
ggplot(aes(x = datetime1, y = "WaterLevel"), data = subds) + geom_line()

有没有一种方法可以在Y轴上每2小时显示一次时间?

1 个答案:

答案 0 :(得分:0)

因为时间在x轴上,所以不清楚“在Y轴上以2小时为间隔的时间”是什么意思。这是将在X轴上的休息时间更改为2小时的示例。 datetime1必须在POSIXct类中。

library(ggplot2)
library(scales)

subds$datetime1 <- as.POSIXct(subds$Date)

ggplot(aes(x = datetime1, y = WaterLevel), data = subds) + 
  geom_line() +
  scale_x_datetime(breaks = date_breaks("2 hours"),
                   labels = date_format("%H:%M"))

enter image description here