rplotlyjitter

Plotly R: jitter point but without the boxplot


I would like to obtain exactly the same result as the one presented here in the best answer of this post: Add jitter to box plot using markers in plotly, but without the boxplot itself keeping only the jitter points. Is there a way to achieve this? thank you for your answers.


Solution

  • Remove the add_trace. Using the literal code from that answer,

    library(plotly)
    set.seed(42)
    dat <- data.frame(xval = sample(100,1000,replace = TRUE),
                      group = as.factor(sample(c("a","b","c"),1000,replace = TRUE)))
    
    dat %>%
      plot_ly() %>% 
      # add_trace(x = ~as.numeric(group),y = ~xval, color = ~group, type = "box", 
      #           hoverinfo = 'name+y') %>%
      add_markers(x = ~jitter(as.numeric(group)), y = ~xval, color = ~group,
                  marker = list(size = 6),
                  hoverinfo = "text",
                  text = ~paste0("Group: ",group,
                                 "<br>xval: ",xval),
                  showlegend = FALSE) %>% 
      layout(legend = list(orientation = "h",
                           x =0.5, xanchor = "center",
                           y = 1, yanchor = "bottom"
                           ),
             xaxis = list(title = "Group",
                          showticklabels = FALSE))
    

    We see:

    plotly jittered scatterplot