How can I combine a line and scatter on same plotly chart?

前端 未结 3 607
说谎
说谎 2021-01-22 02:24

The two separate charts created from data.frame work correctly when created using the R plotly package.
However, I am not sure how to combine them into one (presumably with

3条回答
  •  醉酒成梦
    2021-01-22 03:03

    There are two main ways you can do this with plotly, make a ggplot and convert to a plotly object as @MLavoie suggests OR as you suspected by using add_trace on an existing plotly object (see below).

    library(plotly)
    
    #data
    df <- data.frame(season=c("2000","2000","2001","2001"), game=c(1,2,1,2),value=c(1:4))
    
    #Initial scatter plot
    p <- plot_ly(df, x = game, y = value, mode = "markers", color = season)
    
    #subset of data
    df1 <- subset(df,season=="2001")
    
    #add line
    p %>% add_trace(x = df1$game, y = df1$value, mode = "line")
    

提交回复
热议问题