繁体   English   中英

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

[英]Add jitter to box plot using markers in plotly

我制作了一个箱形图:

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)

在此输入图像描述

但问题是抖动的点不是交互式的,不能单独标记,所以我想到了使用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))

在此输入图像描述

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

我发现自己在同潜在情况往往与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))

产量如下

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM