Something is wrong with my ploty code inside shiny?

15 views
Skip to first unread message

Raju

unread,
Mar 15, 2021, 7:48:18 AM3/15/21
to Shiny - Web Framework for R
How can I make the following chart in plotly work in Shiny?

data1 <-  data  %>% filter(Name == 'Person1') 
  mutate(Date = dmy(Date))

chart <-  plot_ly(data = data1,
                 x = ~ Date)



chart <- chart %>%  add_lines(y= ~Sugar_Post,name = "Sugar Post Test", type = 'scatter', mode = 'markers', line = list(color = 'rgb(122,196,30)'))
chart <- chart %>%  add_lines(y= ~Sugar_Post,name = "Sugar Post Test", type = 'scatter', mode = 'markers', line = list(color = 'rgb(202,96,30)'))
chart <- chart %>%  add_lines(y= ~Sugar_Fast,name = "Sugar Fasting", type = 'scatter', mode = 'markers',line = list(color = 'rgb(220,96,130)'))
chart

The same chart made with ggplot2 worked in shiny

  data1 <-  data  %>%
            mutate(Date = dmy(Date))
    
    output$Plot <- renderPlot({
        
            data1 %>% filter(Name == input$Name)%>%
            ggplot(aes(x= Date))+  
            geom_line(aes(y= Sugar_Fast), color= "Blue")+ geom_point(aes(y= Sugar_Fast,col= "Blue"),size = 3 )+ 
            geom_line(aes(y= Sugar_Post), color = "Red")+ geom_point(aes(y= Sugar_Post,  col = "Red"), size = 3 )+
            geom_line(aes(y= weight), color = "Brown")+ geom_point(aes(y= weight ,  col = "Brown"), size = 3)+
            scale_color_identity(name = "Points", 
                                     breaks = c("Blue", "Red", "Brown"),
                                 labels = c("Sugar_Fasting", "Sugar_Post", "weight"),
                                 guide = "legend")+
            
            labs(title = paste('Details of Sugar Test Score for', input$Name), 
                 subtitle = 'Please select the name from dropdown menu',
                 x =  'Date', 
                 y = 'Test Scores')
        
        
        
    })
    
How should I write the code for plotly? 
Reply all
Reply to author
Forward
0 new messages