5

I'd like the interactive (that means they can be selected with box/lasso selection) jittered points to be displayed on the grouped boxplot. I came out of this question: Add jitter to box plot using markers in plotly. I want exactly the same but the boxplots should be grouped.

I made a boxplot but the points are all 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 making the X axis grouped by factor (so that each combination is a level) I cannot make my boxplot grouped:

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 on X axis, I get the points away from the boxplots:

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?

potockan
  • 3,998
  • 3
  • 25
  • 37

2 Answers2

3


does boxpoints works for for you? See this
You can move those points by pointpos parameter.

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")
  • That works but I want the jittered points to be "more jittered". Is that possible? I changed the value of jitter parameter but not much changes... – potockan Nov 14 '17 at 15:51
  • "jitter" parameter is a percentage of box width, so I am afraid that this simple solution is limited. – Krzysztof Przygodzki Nov 14 '17 at 16:00
  • The problem with that solution is that the points are not interactive and you cannot select the points (with box/lasso select). With add_markers - you can. – potockan Nov 14 '17 at 16:02
1

You could create a ggplot2 object and then make it interactive using ggplotly() function.

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