Plotly R-将yaxis标签从数字转换为定义的时间或数字

时间:2019-01-03 16:12:33

标签: r plotly r-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))

这是一张图片,您可以看到其布局:

Box Plot

1 个答案:

答案 0 :(得分:0)

也许有更好的方法可以做到这一点,但是我在“ 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))

y-axis labels