gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:29:59 27 4
gpt4 key购买 nike

我有几个组的测量值,我想将它们绘制为 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com