当仅绘制1个条形时,频率与POSIXct日期条形图上的设置范围将以R形式破坏

时间:2019-03-28 11:39:58

标签: r plotly posixct r-plotly

我正在用图表以条形图的形式绘制仪器每天测量的数据文件数。

此图表用于闪亮的应用程序中,日期范围,每天的文件数等均取决于用户对数据进行的选择,因此我需要在两种情况下都可以使用它如下所述

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

在绘制2天或更长时间(无论是否设置范围)时,该图都非常有用

range = c(dateRangeMin,dateRangeMax)中的

layout()正在设置范围,并且似乎在 1 bar场景中破坏了绘图。

日期采用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
         ))

1 bar plot

场景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

2 bar plot

在我的应用程序中,情节看起来像这样,并且除了1 bar的情况外,一切都可以正常工作... enter image description here

1 个答案:

答案 0 :(得分:0)

不确定我是否正确。您需要以小节宽度玩。对于条数= 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)
             )