Reputation: 489
anybody can help? trying to create plotly scatter chart where I can change color of selected markers. I checked the dataframe source and indt$active is a num, not factor, yet plotly chart interprets it as a factor
see the code, everything is fine until i set color = ~active
. I tried using inside the reactive indt$active <- as.numeric(as.character( indt$active ))
but it still does nothing. I am struggling to find what factor the chart fails on. It fails to load (you can comment out the color=~active to see without the error). To select values, you need to draw a box
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)
Upvotes: 2
Views: 1902
Reputation: 815
In your plot_ly function try using color = ~as.character(active)
instead of color = ~active
Upvotes: 3