0

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.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • This same question was asked [in a comment](https://stackoverflow.com/questions/46844599/add-jitter-to-box-plot-using-markers-in-plotly#comment90011734_46847387) under the answer to that question, and [immediately answered](https://stackoverflow.com/questions/46844599/add-jitter-to-box-plot-using-markers-in-plotly#comment90012585_46847387). – r2evans Feb 10 '23 at 22:13
  • FYI, I'm closing this as a dupe since nothing here is new given all of the context in that other answer (i.e., comments). Since it's been there longer and has all background information, it should be the default to percolate to the top of a search when somebody asks for something similar. – r2evans Feb 10 '23 at 22:40

1 Answers1

0

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

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Is there a way to add the group name on the x Axis? –  Lemniscomys Feb 10 '23 at 22:54
  • Sure, remove `showticklabels = FALSE` and you get a continuous x-axis labeling. From there I suggest reading the source docs at https://plotly.com/r/axes/, far more complete (and I'm not very proficient in plotly). – r2evans Feb 11 '23 at 14:38