我希望 交互式 (这意味着可以使用框/套索选择来选择它们) 抖动点 显示在 分组箱线图 上。我从这个问题中走出来: Add jitter to box plot using markers in plotly 。我想要完全相同,但箱线图应该分组。

我做了一个箱线图,但点都混淆了:

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

r - 使用 R plotly 中的标记将抖动添加到分组箱线图-LMLPHP

当我尝试将 X 轴按因子分组(这样每个组合都是一个级别)时,我无法将我的箱线图分组:
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")

r - 使用 R plotly 中的标记将抖动添加到分组箱线图-LMLPHP

当我尝试在 X 轴上混合变量时,我从箱线图中得到了点:
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")

r - 使用 R plotly 中的标记将抖动添加到分组箱线图-LMLPHP

有任何想法吗?

最佳答案

您可以创建一个 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')

关于r - 使用 R plotly 中的标记将抖动添加到分组箱线图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47286911/

10-12 23:47