5

我正在尝试在散点图中更改颜色条的标题。

查看文档,似乎这应该是微不足道的。但是,我尝试将其插入colorbar = list(title = "Typing Rate")到主plotly()代码中,以及将其插入到layout()中,如下所示。

如何自定义此标题?

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)
4

2 回答 2

9

尝试这个:

  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)
  })

更新(使用plotly_4.7.1)。也可以这样做:

  output$mainPlot <- renderPlotly({     
        plot_ly(data=filteredFull(), x=x, y=y,
                       color=Rate, size=Rate,
                       mode="markers") %>% colorbar(title = "Typing Rate")
  })
于 2016-05-17T21:03:35.607 回答
2

我在 javaScript 中寻找它时来到了这里。我知道 OP 在 R 中要求它;我正在为那些想在 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,
         },
 ];
于 2017-06-16T06:43:35.330 回答