当只有 1 个条形图要绘制时,频率与 POSIXct 日期条形图的设置范围与 R plotly 断开

Setting range on a frequency vs POSIXct date bar chart breaks with R plotly when there is only 1 bar to plot

我正在使用 plotly 以条形图形式绘制仪器测量的数据文件数量作为每天的频率。

这个图是在一个闪亮的应用程序中使用的,日期范围、每天的文件数量等等都取决于用户在数据方面所做的选择,所以我需要它适用于下述两种情况

两个图,1个相同的代码块,唯一的区别是条数是1或>1。 1 个条形图损坏,>1 个条形图工作正常,对我来说完全没有意义!

无论是否设置范围,绘制 2 天或更多天时,绘图效果都很好

layout() 内的

range = c(dateRangeMin,dateRangeMax) 正在设置范围,似乎打破了 1 柱场景 中的情节。

日期是POSIXct格式,'%Y-%m-%d %H:%M:%S',我设置的也是这种格式

当我没有设置范围时,只有 1 天的数据存在时,该图也有效。一旦我将范围设置为大于 8 毫秒(自动缩放),绘图就会正确缩放,但是那一天的条形图消失了,这完全没有意义。

情景 1:损坏

library(plotly)
#make a dataframe with 1 full date
dayPOSIXct <- as.POSIXct("2016-05-20 19:03:36",format = '%Y-%m-%d %H:%M:%S')
# 1 frequency value (nr of samples
frequency <- 1
# reduce full date to 1 day for counting samples per day
day <- format(as.POSIXct(dayPOSIXct, format = '%Y-%m-%d %H:%M:%S'), format = '%Y-%m-%d')
# give a tag for coloring (files can be in or out of selected date range
datecoloring <- "IN"
# put it together in a dataframe
MyDF_dayfreq <- data.frame(day = day, frequency = frequency, dayPOSIXct = dayPOSIXct, datecoloring = datecoloring)

# calculate the min and max dates of the dataframe for setting range in the plot
# I add 14 hours to the max, and subtract 14 hours from the min to widen the range of the plot  
dateRangeMin <- as.POSIXct( min(MyDF_dayfreq$dayPOSIXct), format = '%Y-%m-%d')-14*3600
dateRangeMax <- as.POSIXct( max(MyDF_dayfreq$dayPOSIXct), format = '%Y-%m-%d')+14*3600

p <-  plot_ly(data = MyDF_dayfreq, source = 'DateRangeHisto',  x = ~dayPOSIXct, y =  ~frequency,   type = "bar",
              color  = as.factor(MyDF_dayfreq$datecoloring), colors = c('#339fff', '#eaf5ff'),
              opacity= 0.5, showlegend = T,
              marker = list(line = list(width = 2, color = '#0000ff')),
              hoverinfo = "text",
              text = ~paste('Files:', MyDF_dayfreq$frequency, '<br>Date:', format(MyDF_dayfreq$day, format = '%Y-%m-%d'), sep = ' '))%>%

  layout(xaxis = list(title = "Date", showgrid = F, showline = T, range = c(dateRangeMin,dateRangeMax), list(tickformat = 'yyyy-mm-dd')),
         yaxis = list(title = "File count", showgrid = F, showline = T, range =c(0,2), tickformat=',d' ),
         margin = list(
           r = 10,
           t = 10,
           b = 40,
           l = 20
         ))

情景 2:工作

dayPOSIXct <- as.POSIXct(c("2016-05-20 19:03:36", "2016-05-21 19:03:36"), format = '%Y-%m-%d %H:%M:%S')
frequency <- c(1,1)
day <- format(as.POSIXct(dayPOSIXct, format = '%Y-%m-%d %H:%M:%S'), format = '%Y-%m-%d')
MyDF_dayfreq <- data.frame(day = day, frequency = frequency, dayPOSIXct = dayPOSIXct, datecoloring = datecoloring)
dateRangeMin <- as.POSIXct( min(MyDF_dayfreq$dayPOSIXct), format = '%Y-%m-%d')-14*3600
dateRangeMax <- as.POSIXct( max(MyDF_dayfreq$dayPOSIXct), format = '%Y-%m-%d')+14*3600

p <-  plot_ly(data = MyDF_dayfreq, source = 'DateRangeHisto',  x = ~dayPOSIXct, y =  ~frequency,   type = "bar",
              color  = as.factor(MyDF_dayfreq$datecoloring), colors = c('#339fff', '#eaf5ff'),
              opacity= 0.5, showlegend = T,
              marker = list(line = list(width = 2, color = '#0000ff')),
              hoverinfo = "text",
              text = ~paste('Files:', MyDF_dayfreq$frequency, '<br>Date:', format(MyDF_dayfreq$day, format = '%Y-%m-%d'), sep = ' '))%>%

  layout(xaxis = list(title = "Date", showgrid = F, showline = T, range = c(dateRangeMin,dateRangeMax), list(tickformat = 'yyyy-mm-dd')),
         yaxis = list(title = "File count", showgrid = F, showline = T, range =c(0,2), tickformat=',d' ),
         margin = list(
           r = 10,
           t = 10,
           b = 40,
           l = 20
         ))
p

在我的应用程序中,情节看起来像这样,一切正常,除了 1 条的情况......

不确定我是否理解正确。你需要玩酒吧宽度。对于条数 = 1,有条但非常细,您需要放大才能看到它。但是您可以使用参数正确设置固定宽度或比例:

        dateRangeMin <- as.POSIXct( min(MyDF_dayfreq$dayPOSIXct), format = '%Y-%m-%d')-1*36
    dateRangeMax <- as.POSIXct( max(MyDF_dayfreq$dayPOSIXct), format = '%Y-%m-%d')+1*36

    p <-  plot_ly(data = MyDF_dayfreq, source = 'DateRangeHisto', 
                  color  = as.factor(MyDF_dayfreq$datecoloring), colors = c('#339fff', '#eaf5ff'),
                  opacity= 0.5, showlegend = T,
                  marker = list(line = list(width = 2, color = '#0000ff')),
                  hoverinfo = "text",
                  text = ~paste('Files:', MyDF_dayfreq$frequency, '<br>Date:', format(MyDF_dayfreq$day, format = '%Y-%m-%d'), sep = ' '))%>%


####################################    update
    add_bars( x = ~dayPOSIXct, y =  ~frequency,   type = "bar", width=20000 
              )%>%
 ##################################     
      layout(xaxis = list(title = "Date"#, showgrid = F# , showline = T
                          , range = c(dateRangeMin,dateRangeMax)#, list(tickformat = 'yyyy-mm-dd')
                          ),
             yaxis = list(title = "File count"#, showgrid = F, showline = T#, range =c(0,2), tickformat=',d' 
                          )
             #,
             #margin = list(r = 10,t = 10,b = 40,l = 20)
             )