【问题标题】:Formatting time on a Y axis in ggplott2在ggplot2中的Y轴上格式化时间
【发布时间】:2018-10-15 03:58:35
【问题描述】:

我有以下代码:

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 小时间隔显示时间?

【问题讨论】:

    标签: r ggplot2 time-series


    【解决方案1】:

    不清楚“Y 轴上以 2 小时为间隔的时间”是什么意思,因为时间在 X 轴上。这是一个将 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"))
    

    【讨论】:

      猜你喜欢
      • 2012-07-29
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      相关资源
      最近更新 更多