R 更改绘图颜色栏标题

R 更改绘图颜色栏标题,r,shiny,plotly,r-plotly,R,Shiny,Plotly,R Plotly,我正在尝试更改绘图仪散点图中颜色栏的标题 看看文档,这似乎是微不足道的。但是,我尝试将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),

我正在尝试更改绘图仪散点图中颜色栏的标题

看看文档,这似乎是微不足道的。但是,我尝试将
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)
库(dplyr)
图书馆(闪亮)
图书馆(绘本)
df试试这个:

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

我在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,
         },
 ];
var plotData8kW=[{
z:8千瓦,
hoverinfo:“z”,
色条:{
//len:0.35,//更改棒材尺寸
标题:“速度(RPM)”,//设置标题
标题栏:'top',//设置位置
//滴答声:[0,50100],
标题字体:{color:'blue'}//标题字体颜色
},
类型:'热图',
色阶:增强色阶,
},
];

Wow,这样就行了!知道为什么吗?假设,它看起来应该和放在
plot_ly()
中一样,我还想解释一下为什么会这样。@Matt,请参阅更新。当您按照OP建议运行代码时,您将看到
'layout'对象没有属性:“colorbar”
。您需要使用
标记定义它。现在,您只需在调用结束时使用
colorbar
即可直接定义它。
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,
         },
 ];