简体   繁体   English

在R中按天绘制x轴

[英]plotting x-axis by days in R

I have a sequence as follows 我有一个序列如下

ts <- data.frame(seq.POSIXt(as.POSIXlt("2018-07-14 00:00"), as.POSIXlt("2018-07-16 13:52"), by="min"))
names(ts)[1]="Timestamp"
ts$Timestamp=format(ts$Timestamp, "%Y-%m-%d %H:%M")

values=rnorm(3713)

I am trying to generate a graph in r-bokeh such that the xaxis only displays the days (not the hours/minutes). 我正在尝试在r-bokeh中生成图形,以使xaxis仅显示日期(而不是小时/分钟)。

I have tried 我努力了

   figure() %>% ly_lines(ts, values) %>% x_axis(label = "Date", format = list(months = "%Y-%m", days = "%d"))

But it hangs. 但它挂了。 I have also tried days="%Y-%m-%d" but no sucess either. 我也尝试过days =“%Y-%m-%d”,但也没有成功。 Any thoughts on how I can generate a line plot for a time series, such that for the x-axis the formatting shows only the days rather than each minute. 关于如何生成时间序列折线图的任何想法,例如对于x轴,格式仅显示天,而不是每分钟。 I am open to a ggplot solution as well. 我也对ggplot解决方案持开放态度。

Thanks! 谢谢!

Here you go! 干得好!

library(tidyverse)

ts <- data.frame(seq.POSIXt(as.POSIXlt("2018-07-14 00:00"), as.POSIXlt("2018-07-16 13:52"), by="min"))
names(ts)[1]="Timestamp"
ts$Timestamp=format(ts$Timestamp, "%Y-%m-%d %H:%M")

values=rnorm(3713)

plot_df <- cbind(ts, values) %>% 
  mutate(time = as.POSIXct(Timestamp, format = "%Y-%m-%d %H:%M"))

plot_df %>% 
  ggplot(aes(x = time, y = values)) + 
  geom_line()

你的阴谋!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM