使用plotly中的标记将抖动添加到箱形图中 [英] Add jitter to box plot using markers in plotly

查看:216
本文介绍了使用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添加这些点的方法:

but problem is that jittered points are not interactive and cannot be marked separately, so I came with an idea to add those points using 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的坐标.有什么想法吗?

but now points are in straight line and I'd like to add some jitter (for example by using beeswarm package). But I don't know how to get coordinates of qualitative variable IC0 on X axis. Any ideas?

推荐答案

我经常使用plotly和ggplot2-处于相同的潜在情况下-3行代码可获得90%的所需内容和30行代码正确地获得美学效果.

I find myself in the same potential case often with plotly and ggplot2-- 3 lines of code to get 90% of what I want, and 30 lines of code to get the aesthetics just right.

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

One potential solution/workaround: Take advantage of R's "factors are coded with integers" paradigm, plot everything on a numeric scale, and then cover your tracks by hiding x labels and x hover values.

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

产生以下

这篇关于使用plotly中的标记将抖动添加到箱形图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆