标绘R:抖动点,但无箱形图[重复]

643ylb08  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(79)

此问题在此处已有答案

Add jitter to box plot using markers in plotly(1个答案)
3天前关闭。
我想获得完全相同的结果,作为一个在这里提出的最佳答案这篇文章:Add jitter to box plot using markers in plotly,但没有箱线图本身只保留抖动点。有什么方法可以实现这一点吗?谢谢您的回答。

vuktfyat

vuktfyat1#

删除add_trace。使用that answer中的文字代码,

library(plotly)
set.seed(42)
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))

我们看到:

相关问题