Skip to content Skip to sidebar Skip to footer

Add Shaded Rectangle With Select Box Corners In Plotly R

I am trying to create an application where if a user employs the Box Select tool in Plotly, then a filled rectangle will appear alongside the Box Select. To accomplish this, I am t

Solution 1:

Basically, strange as it may sound to us old ggplot vets, "shapes" are not handled the same way as markers and lines (which are set in "traces"), but "shapes" (like rect) are set in the layout part of a plotly plot. Probably I would understand this better if I read more of the plotly book..

So I made two changes.

  • Changed the dragmode to "select" using layout at the begining.
  • Changed the Plotly.addTraces logic at the end of your onRender javascript to Plotly.relayout

As a sidenote I have been wondering for hours why it was called "relay-out", I just now realized as I was writing this that it is "re-layout" which makes complete sense.

So here is the code:

library(ggplot2)library(shiny)library(plotly)library(htmlwidgets)ui<-basicPage(plotlyOutput("plot1"))server<-function(input,output) {

  p<-ggplot(mtcars, aes(x=wt, y=mpg))+geom_point(alpha=0)+xlim(0,5)+ylim(-3,3)gp<-ggplotly(p)set.seed(3)myDF<-data.frame(X1=rnorm(10,-1), X2=rnorm(10,-1), X3=rnorm(10,-1), X4=rnorm(10,1), X5=rnorm(10,1), X6=rnorm(10,1))colNms<-colnames(myDF)nVar<-length(colNms)output$plot1<-renderPlotly({
    gp%>%layout(dragmode="select")%>%onRender("function(el, x, data) {

                    varmyDF=data.myDFvarTraces= [];vardLength=myDF.lengthvarvLength=data.nVarvarcNames=data.colNmsfor(a=0;a<dLength;a++){
                    xArr= [];yArr= [];for(b=0;b<vLength;b++){
                    xArr.push(b)yArr.push(myDF[a][cNames[b]]);
                    }
                    varpcpLine= {
                    x:xArr,
                    y:yArr,
                    mode:'lines',
                    line: {
                    color:'orange',
                    width:1
                    },
                    opacity:0.9,
                    }
                    Traces.push(pcpLine);
                    }
                    Plotly.addTraces(el.id, Traces);el.on('plotly_selected', function(e) {
                    vardLength=myDF.lengthvarselectedPCP= []
                    varxMin=e.range.x[0]
                    varxMax=e.range.x[1]
                    varyMin=e.range.y[0]
                    varyMax=e.range.y[1]

                    console.log([xMin, xMax, yMin, yMax])varTraces= []
                    vardrawRect= {
                    type:'rect',
                    x0:xMin,
                    y0:yMin,
                    x1:xMax,
                    y1:yMax,
                    line: {
                    color:'green',
                    width:1
                    },
                    fillcolor:'green'
                    }
                    varupdate= {
                        shapes:[drawRect]
                     }
                    Plotly.relayout(el.id, update)
                    })
                    }", data = list(myDF = myDF, nVar = nVar, colNms = colNms))})

  }
shinyApp(ui, server)

And here is a screen shot of it working:

enter image description here

Post a Comment for "Add Shaded Rectangle With Select Box Corners In Plotly R"