Prevent glitter from updating conspiracy elements

I am trying to keep the scale and scale size of my chart constant, no matter what points are actually displayed.

In ggplot I could set these constants using arguments like scale_color_gradient(limits=c(0,1)) . However, I cannot find a parallel function in plot_ly and for other reasons I cannot use ggplotly() here.

I believe that this can also be done using eventReactive() , but it’s hard for me to figure out how to use it.

Here is a minimal example in which the color and size of the plot continue to change.

 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) 

Is it possible to update only those points that are displayed without updating the scale / color range / size?

+5
source share
1 answer

The carnal also has color restrictions. They are nested under the marker attribute. You can set the maximum and minimum color scale values ​​(numeric values) to leave a constant scale regardless of which dots are actually displayed.

I just added marker = list(cmin = 1, cmax = 8) to the plot_ly command. Therefore, you will need to scan your df to determine what your maximum and minimum values ​​are.

Code below:

 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) 
+5
source

All Articles