简体   繁体   中英

Shiny Plotly multiple smoothed lines based on color

thank you in advance for the help. I am trying to use plotly within a shiny app I have. I am a bit new to plotly so I apologize if this is a no brainer. I would like to use a scatterplot that paints each point a different color based on input$Col, then for each group within input$Col I would like to draw a smoothed line for each color. Everything works fine except for the fact that when I use add_lines() it just draws one line, and does not differentiate for the different colors that are on the scatterplot.

 plot_ly(poolfinderdata1(), type = "scatter", x = ~get(input$X), 
      y = ~get(input$Y),
      mode = "markers",
      color = ~get(input$Col),
      symbol = as.factor(poolfinderdata1()$Matcher))%>%
      add_lines(y = ~fitted(loess(get(input$Y) ~ get(input$X)), 
 color ~  get(input$Col)))%>%
 layout(xaxis =list(title= input$X), yaxis = list(title = input$Y))

ggplot2 has been around for a long time so it already has a bunch of stuff you can use and I think learning it it's worth the effort, specially since you can turn your plots interactive with the ggplotly function. Here's one way to solve your problem:

library(plotly)
library(ggplot2)
data <- poolfinderdata1()
data[[input$Col]] <- as.factor(data[[input$Col]]) # so the color is mapped as discrete
data$Matcher <- as.factor(data$Matcher) # same for the markers
p <- ggplot(data, aes_string(x = input$X, y = input$Y, color = input$Col)) +
     geom_point(aes(shape = Matcher)) +
     geom_smooth(method = "loess")
ggplotly(p)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM