【问题标题】:Change Plotly Colorbar Title更改绘图颜色栏标题
【发布时间】:2016-09-13 23:15:49
【问题描述】:

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

查看文档,似乎这应该是微不足道的。但是,我尝试将 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)

【问题讨论】:

    标签: r shiny plotly r-plotly


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 哇,原来如此!知道为什么吗?假设,它似乎应该与将其放入plot_ly()
    • 我还想解释一下为什么会这样。
    • @Matt,查看更新。当您按照 OP 的建议运行代码时,您将看到 'layout' objects don't have the attribute: 'colorbar'。您需要使用marker 定义它。您现在可以在通话结束时使用colorbar 直接定义它。
    【解决方案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,
             },
     ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 2013-04-20
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多