Add jitter to the field using markers in the plot

I made a box:

dat %>%
  plot_ly(y = ~xval, color = ~get(col), type = "box", 
          boxpoints = "all", jitter = 0.7,
          pointpos = 0, marker = list(size = 3),
          source = shiny_source, key = shiny_key,
          hoverinfo = 'text', text = txt)

enter image description here

but the problem is that the jittering points are not interactive and cannot be marked separately, so I came up with the idea to add these points using add_markers:

dat %>%
  plot_ly(y = ~xval, color = ~get(col), type = "box", 
          boxpoints = FALSE, jitter = 0.7,
          pointpos = 0, marker = list(size = 3),
          source = shiny_source, key = shiny_key,
          hoverinfo = 'col', text = txt
  ) %>%
  add_markers(x = ~get(col), y = ~varval, size = I(6))

enter image description here

but now the points are in a straight line, and I would like to add some jitter (for example, using a package beeswarm). But I don’t know how to get the coordinates of a quality variable IC0along the X axis. Any ideas?

+1
source share
1 answer

I find myself in the same potential case often with plotly and ggplot2 - 3 lines of code to get 90% of what I want, and 30 lines of code to get aesthetics in order.

/ : R " " , , , x x hover.

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))

enter image description here

+3

Source: https://habr.com/ru/post/1689251/


All Articles