Add jitter to a grouped graph using markers in R

I would like interactive (this means that they can be selected with the choice of box / lasso) trembling dots that will be displayed on the grouped strong> box . I got out of this question: Add jitter to the field using markers in the plot . I want exactly the same, but the boxes must be grouped.

I made a box, but all the points mixed up:

dat %>%
  plot_ly(x = ~as.numeric(IC), 
            y = ~xval, 
            color = ~gene, 
            type = "box",
            hoverinfo = "none",
            boxpoints = FALSE
          ) %>%
  add_markers(x = ~jitter(as.numeric(IC)),
              y = ~xval,
              color = ~gene,
              marker = list(size = 3),
              hoverinfo = "text",
              text = txt,
              showlegend = TRUE) %>%
layout(boxmode = "group")

enter image description here

When I try to make the X axis grouped by coefficient (so that each combination is a level), I cannot group my block plotter:

dat <- dat %>% 
  mutate(gene_x_covariate = as.factor(
    paste0(get(facet_title), "-", gene))) 

dat %>%
  plot_ly(x = ~as.numeric(gene_x_covariate), 
            y = ~xval, 
            color = ~gene, 
            type = "box",
            hoverinfo = "none",
            boxpoints = FALSE
          ) %>%
  add_markers(x = ~jitter(as.numeric(gene_x_covariate)),
              y = ~xval,
              color = ~gene,
              marker = list(size = 3),
              hoverinfo = "text",
              text = txt,
              showlegend = TRUE) %>%
layout(boxmode = "group")

enter image description here

When I try to mix the variables along the X axis, I get points from the boxes:

dat %>%
  plot_ly(x = ~as.numeric(IC), 
            y = ~xval, 
            color = ~gene, 
            type = "box",
            hoverinfo = "none"
          ) %>%
  add_markers(x = ~jitter(as.numeric(gene_x_covariate)),
              y = ~xval,
              color = ~gene,
              marker = list(size = 3),
              hoverinfo = "text",
              text = txt,
              showlegend = TRUE) %>%
layout(boxmode = "group")

enter image description here

Any ideas?

+4
2

ggplot2, ggplotly().

library(dplyr)
library(ggplot2)
library(plotly)

dat <- data.frame(xval = sample(100,1000,replace = TRUE),
              group1 = as.factor(sample(c("a","b","c"),1000,replace = TRUE)),
              group2 = as.factor(sample(c("g1","g2","g3","g4"),1000, replace = TRUE)))

p <- dat %>% ggplot(aes(x=group2, y=xval, fill=group1)) + 
              geom_boxplot() + geom_jitter() + facet_grid(~group2)

ggplotly(p) %>% layout(boxmode = 'group')
+1


boxpoints ? .
pointpos.

iris %>% 
  plot_ly(x = ~cut( Sepal.Length, breaks = 4), 
          y = ~Petal.Width, 
          color = ~Species, 
          type = "box",
          marker = list( size = 10),
          boxpoints = "all",
          jitter = 0.4,
          pointpos = 0,
          hoverinfo = "all"
  ) %>% layout( boxmode = "group")
+2

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


All Articles