情节中的颜色 - '范围'对因素没有意义

时间:2017-07-20 14:27:25

标签: r shiny plotly

任何人都可以提供帮助吗?尝试创建绘图散点图,我可以在其中更改所选标记的颜色。我检查了数据帧源,indt $ active是一个num,而不是因子,但plotly chart将其解释为一个因素 看到代码,一切都很好,直到我设置color = ~active。我尝试在反应indt$active <- as.numeric(as.character( indt$active ))内部使用,但它仍然没有做任何事情。我正在努力寻找图表失败的因素。它无法加载(您可以注释掉color =〜active以查看没有错误)。 要选择值,您需要绘制一个框

library(plotly)
library(shiny)
library(dplyr)
library(tidyr)


tms<-format(seq(as.POSIXct("2013-01-01 00:00:00", tz="GMT"), 
                length.out=48, by='30 min'), '%H:%M')
dts<-c( "Day Before BH","BH","Sunday","Saturday","Friday","Thursday","Wednesday","Tuesday", "Monday" )

indt <- as.data.frame(matrix( c(0), nrow=9,  ncol=48, byrow = TRUE))
indt<-cbind(dts,indt)

colnames(indt) <- c("dt",tms)

indt<-gather(indt,tm,active,-dt)

pal <- c("red", "blue")
pal <- setNames(pal, c(0, 1))



ui <- fluidPage(
  plotlyOutput("plot")

)

server <- function(input, output, session) {



  ind<-reactive({
    d <- event_data("plotly_selected")
    indt[d$pointNumber+1,3]<-1
    indt<<-indt
    return(indt)
  })


  output$plot <- renderPlotly({


    f1 <- list(size = 8) 

    yform <- list(title = "",
                  categoryorder = "array",
                  categoryarray = dts
                  ,tickfont = f1)



    plot_ly(ind()
            , x = ~tm, y = ~dt, mode = "markers", type = "scatter", 
            color = ~active, 
            colors = pal,
            marker = list(size = 20)

    ) %>%
      layout(dragmode = "select") %>%
      layout(xaxis = list(title = "", tickfont = f1), 
             yaxis = yform)
  })
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:2)

在plot_ly函数中,尝试使用color = ~as.character(active)代替color = ~active