r - 强制绘图 fiddle 图不在零值上显示 fiddle

标签 r plotly violin-plot

我有几个组的测量值,我想将它们绘制为 fiddle 图:

set.seed(1)
df <- data.frame(val = c(runif(100,1,5),runif(100,1,5),rep(0,100)),
                 group = c(rep("A",100),rep("B",100),rep("C",100)))

使用 Rggplot2 :
library(ggplot2)
ggplot(data = df, aes(x = group, y = val, color = group)) + geom_violin()

我得到:
enter image description here

但是当我尝试使用 R 获得等效结果时的 plotly使用:
library(plotly)
plot_ly(x = df$group, y = df$val, split = df$group, type = 'violin', box = list(visible = F), points = F, showlegend = T, color = df$group)

我得到:
enter image description here

“C”组获得充气/人造 fiddle 。

知道如何处理这个问题而不是使用 ggplotly ?

最佳答案

我没有找到修复 plotly 行为的方法(可能值得为此编写错误报告)。一种解决方法是过滤您的数据以仅在范围大于零的组上绘制 fiddle 图。如果您还需要显示其他组的位置,您可以为这些使用箱线图。

为了演示,我使用 library(data.table)为过滤阶段。如果您愿意,可以使用 dplyr 或相同过程的基本版本:

setDT(df)[, toplot := diff(range(val)) > 0, group]

现在我们可以根据他们是否应该有 fiddle 来使用不同的跟踪样式来绘制组
plot_ly() %>%
  add_trace(data = df[(toplot)], x = ~group, y = ~val, split = ~group, 
            type = 'violin', box = list(visible = F), points = F) %>% 
  add_boxplot(data = df[(!toplot)], x = ~group, y = ~val, split = ~group)

enter image description here

关于r - 强制绘图 fiddle 图不在零值上显示 fiddle ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55011840/

相关文章:

r - 用 geom_violin 填充透明度

r - 在 lm.wfit 中, z <- .Call(C_Cdqrls, x *wts, y*wts, tol) 做什么?

plotly - 在轴坐标之间添加标签

python - 隐藏图例中的图例条目

python - Matplotlib fiddle 图在同一列上重叠

python - 使用python绘制正值的 fiddle 图

r - 如何从函数生成 Rmarkdown 文件

r - 了解 R 中的 switch() 函数?

r - 使用 R 进行网页抓取(抓取隐藏号码 "Click here to show number")

python - Plotly:如何使用 px.line 更改线条样式?