【问题标题】:Prevent shiny from updating elements of plotly防止闪亮更新情节元素
【发布时间】:2016-05-16 21:43:16
【问题描述】:

无论实际显示哪些点,我都试图保持绘图的颜色比例和大小比例不变。

ggplot 中,我可以使用scale_color_gradient(limits=c(0,1)) 之类的参数设置这些常量。但是,我在plot_ly 中找不到并行函数,并且由于其他原因,我无法在此处使用ggplotly()

我相信eventReactive() 也可以做到这一点,但我无法理解如何使用它。

这是一个最小的示例,绘图颜色和大小不断变化。

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

shinyApp(ui, server)

是否可以只更新显示的点,而不更新颜色/大小的比例/范围?

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    Plotly 也有这些色阶限制。它们嵌套在标记属性下。您可以设置色标的最大值和最小值(数值)以保持色标不变,而不管实际显示哪些点。

    我刚刚在plot_ly 命令中添加了marker = list(cmin = 1, cmax = 8)。因此,您必须扫描您的df 以确定您的最大值和最小值。

    代码如下:

    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"),
          plotlyOutput("mainPlotFixedSize")
        )
      )
    ))
    
    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', marker = list(cmin = 1, cmax = 8))
      })
    
      output$mainPlotFixedSize <- renderPlotly({
        plot_ly(data=filteredFull(), x=x, y=y,
          color=Rate, mode='markers', 
          marker = list(sizemode = "area", size = Rate*3400, cmin = 1, cmax = 8))
      }) 
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这对于色标来说非常棒!谢谢!至于尺寸呢?我在文档中找不到任何内容。
    • 有点晚了,但我编辑了答案以包含大小。控制大小要困难得多。这实际上很容易,但您必须手动调整所有尺寸。我通过实验选择了速率因子(最小 3400 像素区域)。
    • @Adam_G 您能否检查一下更新后的答案是否适合您?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2021-05-10
    • 2014-09-12
    • 1970-01-01
    • 2018-04-03
    • 2017-08-12
    相关资源
    最近更新 更多