6

I am trying to change the title of the colorbar in a plotly scatter plot.

Looking at the documentation, it seems this should be trivial. However, I've tried inserting colorbar = list(title = "Typing Rate") into both the main plotly() code, as well as piping it into layout(), as below.

How can I customize this title?

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

df <- as.data.frame(list("UserID"=c(1,1,1,1,2,2,2,2), 
                          "Category"=c('A','A','B','B','A','A','B','B'),
                          "Rate"=c(2,3,5,6,8,6,7,1),
                          "x"=c(1,3,5,7,2,4,6,8),
                          "y"=c(1,3,5,7,2,4,6,8)
                    ))

ui <- (fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("userInput","Select User", sort(unique(df$UserID)),
                  selected=1),
      checkboxGroupInput("CategoryInput", "Select Category", sort(unique(df$Category)),
                         selected=c('A','B'))
    ),

    mainPanel(
      plotlyOutput("mainPlot")#,
    )
  )
))

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

  # filter for both user and category
  filteredFull <- reactive({
      df %>% 
        filter(
          UserID == input$userInput,
          Category %in% input$CategoryInput
        )
  })

  output$mainPlot <- renderPlotly({
        plot_ly(data=filteredFull(), x=x, y=y,
                       color=Rate, size=Rate,
                       mode='markers') %>%
      layout(
        colorbar = list(title = "Typing Rate")
      )
  })
}

shinyApp(ui, server)
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Adam_G
  • 7,337
  • 20
  • 86
  • 148

2 Answers2

9

Try this:

  output$mainPlot <- renderPlotly({     
    m <- list(
  colorbar = list(title = "Typing Rate")
)
        plot_ly(data=filteredFull(), x=x, y=y,
                       color=Rate, size=Rate,
                       mode="markers", marker=m)
  })

Update (with plotly_4.7.1). It could also be done that way:

  output$mainPlot <- renderPlotly({     
        plot_ly(data=filteredFull(), x=x, y=y,
                       color=Rate, size=Rate,
                       mode="markers") %>% colorbar(title = "Typing Rate")
  })
MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Wow, so that works! Any idea why? Hypothetically, it seems like it should be the same thing as putting it in `plot_ly()` – Adam_G May 18 '16 at 01:28
  • I would also like some explanation for why this works. – Matt May 07 '18 at 22:08
  • @Matt, see the update. When you run the code as the OP suggested you will see that `'layout' objects don't have the attribute: 'colorbar'`. You need to define it with `marker`. You can now directly define it simply using `colorbar` at the end of your call. – MLavoie May 07 '18 at 22:34
2

I ended up here while looking for it in javaScript. I know OP asked for it in R; I am adding this answer for those who want to do the same in JS.

var plotData8kW = [{
       z: data8kW,
        hoverinfo:"z",
        colorbar:{
            //  len: 0.35, //Change size of bar
            title: 'Speed(RPM)<br\><br\>', //set title
            titleside:'top', //set postion
           //tickvals:[0,50,100],
            titlefont: {color: 'blue'} //title font color
          },
         type: 'heatmap',
         colorscale: enhancedScale,
         },
 ];
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154