【问题标题】:colors in plotly - ‘range’ not meaningful for factors情节中的颜色-“范围”对因素没有意义
【发布时间】:2017-07-20 14:27:25
【问题描述】:

有人可以帮忙吗?试图创建散点图,我可以在其中更改所选标记的颜色。我检查了数据框源,并且 indt$active 是一个数字,而不是因素,但绘图图表将其解释为一个因素 查看代码,一切都很好,直到我设置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)

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    在你的 plot_ly 函数中尝试使用color = ~as.character(active)而不是color = ~active

    【讨论】:

    • 再简单不过了。在我问我是否尝试过 'color = ~as.integer(as.character(active))' 之前,它不起作用,我不知道为什么,所以我在其他地方寻找错误
    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多