0

I must design a graph that accumulates variables as they are added in Shiny R using plotly.

For example, if I graph the variable x with respect to the date t with a select input, I add the variable and it is located on the right side of the variable x, indicating with a separator that it is the variable y and so with as many variables are selected.

This is my code:

library(shiny)
library(plotly)
library(dplyr)


set.seed(123)
df <- data.frame(x = seq.Date(as.Date("2000/1/1"), by = "month", length.out = 100),
                 cat = sample(c("m1","m2","m3"),100, replace = TRUE),
                 a = cumsum(rnorm(100)),
                 b = rnorm(100),
                 c = rnorm(100),
                 d = rnorm(100))

ui <- fluidPage(
  selectInput("x","Variable",names(df)[-1],NULL,TRUE),
  selectInput("y", "category", unique(df$cat), NULL, TRUE),
  numericInput("ls","limite superior",NULL,-100,100),
  numericInput("li","limite superior",NULL,-100,100),
  plotlyOutput("plot1")
  
)

server <- function(input, output, session) { 
 
    
  output$plot1 <- renderPlotly({    
    req(input$y, input$x)    
    df <- df%>%
      filter(cat %in% input$y)%>%
      select(one_of("x",input$x)) 
    
    estado <- ifelse(df[[2]]>input$ls,"red", 
                     ifelse(df[[2]]<input$ls & df[[2]]>input$li,
                            "orange","green"))
    
    df$estado <- estado 
    
    p <- plot_ly(df,
                 x = ~x,
                 y = ~df[[2]],
                 type = "scatter",
                 mode = "lines") 
    ## Makers
    
    p <- p %>%
      add_trace(x = ~x,
                y= df[[2]],
                marker = list(color = ~estado, size = 20, symbol = "square"),
                showlegend = FALSE)
      
      
      
    ## Lengends and labels
    
    
    p <- p %>%
      layout(legend = list(orientation = 'h'))%>%
      layout(title = paste('Comportamiento de calidad de agua residual', input$estacion, sep=' '), 
             plot_bgcolor = "#e5ecf6", 
             xaxis = list(title = 'Fecha'), 
             yaxis = list(title = paste(input$x,"mg/l", sep=" ")))
    
    print(p)
      
    
  })
    
    
  
}

shinyApp(ui, server)

I need that when adding the variables a, b, c, d, the graph will be made just after the variable that was already there so that it looks something like this:

Example

MatCordTo
  • 223
  • 1
  • 7
  • You may to use plotly proxy, read this [article](https://plotly-r.com/linking-views-with-shiny.html) 17.3.1. This requires you also need to learn [plotly js functions](https://plotly.com/javascript/plotlyjs-function-reference/). Example use of proxy can be found here: https://stackoverflow.com/questions/50620360/using-proxy-interface-in-plotly-shiny-to-dynamically-change-data – lz100 Nov 05 '21 at 19:27
  • Maybe I did not make myself understood well but I already found a solution, but thanks! – MatCordTo Nov 06 '21 at 14:14

1 Answers1

0

Use subplot and do function.

df %>%
 group_by(category) %>%
 do(p = plot_ly(...) %>% (plot_features...)) %>%
 subplot(sharex= FALSE,sharey=TRUE, nrow=1, margin = 0.0001)

With plot feautures i mean all the deatils of the plot (markers, lines, colors, etc)

MatCordTo
  • 223
  • 1
  • 7