Plotly R - 将 yaxis 标签从数字转换为定义的时间或数字
Plotly R - Convert yaxis labels from numbers to defined times or numbers
您将在下面看到我使用相机分析在 Plotly 中创建了一个位置的箱线图。我正在尝试将 y 轴刻度标签从 0 - 1400 转换为时间。所以 0 将是 0000 或 00:00,200 将是 0320 或 03:20,400 将是 0640 或 06:40 等等,直到 1400 将是 2320 或 23:20。
模式实际上是数字除以 60(分钟)。
我的代码:
library('plotly')
df <- read.csv("BoxPlot.csv")
location_one <- df[df$Location == "location_one", ]
plot_ly(location_one, x = ~Date, y = ~Minutes, type = "box", name = 'Camera Activity') %>%
add_trace(y = ~seven,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgba(255,0,0,0.8)'),
showlegend = FALSE) %>%
add_trace(y = ~seventeen,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(255, 0, 0, 0.4)',
line = list(color = 'rgba(255,0,0,0.8)'),
name = 'Business Hours') %>%
add_trace(y = ~six,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgba(255,0,255,0.8)'),
showlegend = FALSE) %>%
add_trace(y = ~endsix,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(255,0,255,0.4)',
line = list(color = 'rgba(255,0,255,0.8)'),
name = 'Extended Business Hours') %>%
add_trace(y = ~starteighteen,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgba(255,0,255,0.8)'),
showlegend = FALSE) %>%
add_trace(y = ~eighteen,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(255,0,255,0.4)',
line = list(color = 'rgba(255,0,255,0.8)'),
showlegend = FALSE) %>%
layout(title = " ",
xaxis = list(title = "Date"),
yaxis = list(autorange = "reversed",
title = "Count"),
margin = list(b = 190, l = 50))
这是一张图片,您可以看到布局:
可能有更好的方法来做到这一点,但我在 "yaxis" 中添加了一些东西,它完成了工作:
layout(title = "location_one",
xaxis = list(title = "Date"),
yaxis = list(autorange = "reversed",
title = "Time",
tickmode = "array",
nticks = 8,
tickvals = c(0,200,400,600,800,1000,1200,1400),
ticktext=c("00:00", "03:20", "06:40", "10:00", "13:20", "16:40", "20:00", "23:20")),
margin = list(b = 190, l = 50))
您将在下面看到我使用相机分析在 Plotly 中创建了一个位置的箱线图。我正在尝试将 y 轴刻度标签从 0 - 1400 转换为时间。所以 0 将是 0000 或 00:00,200 将是 0320 或 03:20,400 将是 0640 或 06:40 等等,直到 1400 将是 2320 或 23:20。
模式实际上是数字除以 60(分钟)。
我的代码:
library('plotly')
df <- read.csv("BoxPlot.csv")
location_one <- df[df$Location == "location_one", ]
plot_ly(location_one, x = ~Date, y = ~Minutes, type = "box", name = 'Camera Activity') %>%
add_trace(y = ~seven,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgba(255,0,0,0.8)'),
showlegend = FALSE) %>%
add_trace(y = ~seventeen,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(255, 0, 0, 0.4)',
line = list(color = 'rgba(255,0,0,0.8)'),
name = 'Business Hours') %>%
add_trace(y = ~six,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgba(255,0,255,0.8)'),
showlegend = FALSE) %>%
add_trace(y = ~endsix,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(255,0,255,0.4)',
line = list(color = 'rgba(255,0,255,0.8)'),
name = 'Extended Business Hours') %>%
add_trace(y = ~starteighteen,
type = 'scatter',
mode = 'lines',
line = list(color = 'rgba(255,0,255,0.8)'),
showlegend = FALSE) %>%
add_trace(y = ~eighteen,
type = 'scatter',
mode = 'lines',
fill = 'tonexty',
fillcolor='rgba(255,0,255,0.4)',
line = list(color = 'rgba(255,0,255,0.8)'),
showlegend = FALSE) %>%
layout(title = " ",
xaxis = list(title = "Date"),
yaxis = list(autorange = "reversed",
title = "Count"),
margin = list(b = 190, l = 50))
这是一张图片,您可以看到布局:
可能有更好的方法来做到这一点,但我在 "yaxis" 中添加了一些东西,它完成了工作:
layout(title = "location_one",
xaxis = list(title = "Date"),
yaxis = list(autorange = "reversed",
title = "Time",
tickmode = "array",
nticks = 8,
tickvals = c(0,200,400,600,800,1000,1200,1400),
ticktext=c("00:00", "03:20", "06:40", "10:00", "13:20", "16:40", "20:00", "23:20")),
margin = list(b = 190, l = 50))