在图中使用标记将抖动添加到框图中

Taz*_*Taz 2 plot r boxplot plotly jitter

我制作了一个箱形图:

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但问题是抖动的点不是交互式的,不能单独标记,所以我想到了一个想法,即使用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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但现在点是直线的,我想添加一些抖动(例如使用beeswarm包).但我不知道如何IC0在X轴上获得定性变量的坐标.有任何想法吗?

Mat*_*ill 8

我发现自己在同潜在情况往往与plotly和ggplot2-- 3行代码来获得我想要的90%,和30行代码来获得美观只是权利.

一个潜在的解决方案/解决方法:利用R的"因子用整数编码"范例,在数字刻度上绘制所有内容,然后通过隐藏x标签和x悬停值来覆盖轨道.

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))
Run Code Online (Sandbox Code Playgroud)

产量如下

在此输入图像描述

  • 是的 - 只需注释掉第一个 `add_trace( ... type = "box" ... )` 调用并将 `add_markers( ... )` 调用中的 `showlegend` 参数更改为 `TRUE` (2认同)