SeG*_*eGa 5 timestamp r plotly
我想用绘图条绘制一些时间戳,其中 1 条表示整整一个小时。
我的问题是刻度线在中间居中,我想将它们移到条形图的左端。
当绘图不放大时,这不是问题,但是放大时,会出现更多刻度标签,它们会出错。
编辑:我需要该选项,barmode = 'overlay'因为我还有其他要绘制的轨迹,这些轨迹未包含在本示例中。
下图说明了我当前和预期的布局,这里有一些数据来制作该图。(我尝试过但没有成功的一些选项也包含在 xaxis 配置中,但未注释)。
library(plotly)
library(lubridate)
df <- data.frame(
ts = seq(as.POSIXct("2019-03-20 00:00:00"), by = "hour", length.out = 24),
val = sample(1:100, 24)
)
plot_ly() %>%
add_bars(data = df, x = ~ts, y = ~val) %>%
layout(dragmode = "select", autosize = TRUE, selectdirection = "h",
barmode = 'overlay',
bargap = 0.05,
xaxis = list(ticks = "outside",
type = "date",
# tickson="boundaries",
# offset=1800,
tickmode = "auto",
title = ""
)) %>%
config(scrollZoom = TRUE)
Run Code Online (Sandbox Code Playgroud)
以下内容能满足您的需求吗?
我有时利用的一件事plotly是,您可以显示不同的值,text这些值独立于用于绘制数据的x和值。y
在本例中,我们可以创建一个包含偏移时间值的列,ts_x并为每行绘制时间过去半小时后的 x 值 - 如果每小时有一列,这会有效地将条形图左对齐。
library(plotly)
df <- data.frame(
ts = seq(as.POSIXct("2019-03-20 00:00:00"), by = "hour", length.out = 24),
val = sample(1:100, 24)
)
## Create a dummy column with x offset values
df$ts_x <- df$ts + 1800
plot_ly() %>%
## Plot based on the dummy column
add_bars(data = df, x = ~ts_x, y = ~val,
## Cover up our tracks by not showing true x value on hoverinfo
hoverinfo = "text",
## Give text that includes the un-altered time values
text = ~paste0("Time: ",format(ts, format = "%B %d, %Y %H:%M"),
"<br>Value: ",val)) %>%
layout(dragmode = "select", autosize = TRUE, selectdirection = "h",
barmode = 'overlay',
bargap = 0.05,
xaxis = list(ticks = "outside",
type = "date",
tickmode = "auto",
title = ""
)) %>%
config(scrollZoom = TRUE)
Run Code Online (Sandbox Code Playgroud)