【问题标题】:How to keep quantile lines in ggplot violin chart when using plotly使用plotly时如何在ggplot小提琴图中保留分位数线
【发布时间】:2019-10-02 15:15:33
【问题描述】:

我正在尝试使用 r plotly 包为我的 ggplot 小提琴图表添加一些交互性。不幸的是,每当我将绘图包裹在ggplotly 中时,我的图表就会消失。具体来说,在创建小提琴图表时,我使用draw_quantiles 参数在图表中绘制水平线。这些行在 ggplot 中正确显示,但在 plotly 输出中不存在。

以下是一个可重现的示例。第一个图表是 ggplot 包含所需的水平线。一旦 ggplot 被 ggplotly 函数包裹,水平线就会消失:

# load the libraries
library(data.table)
library(ggplot2)
library(plotly)

# create example data
DT <- data.table(x= rep(c('a', 'b', 'c'), 1000),
                 y = rpois(3000, lambda = c(2, 3, 5)))

# create violin chart using ggplot
p <- ggplot(DT, aes(x =x, 
               y = y, 
               fill = x)) +
  geom_violin(draw_quantiles = c(0.25, 0.5, 0.75)) 

# wrap ggplot in plotly
ggplotly(p)

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    Plotly 似乎没有等效的几何对象:https://plot.ly/r/violin/

    另一种方法是覆盖箱线图几何。

    ggplot(DT, aes(x =x, y = y, fill = x)) +
      geom_violin(alpha=0.7) +
      geom_boxplot(width=0.2, fill="white")
    

    但是 plotly 忽略了箱线图的宽度,所以它不像原生 ggplot2 那样漂亮。

    所以你必须使用原生的 plotly 语法来更好地将箱线图嵌入到小提琴中。请查阅我提供的链接。

    编辑:

    如果你想让你的生活更复杂,你可以手动计算水平线段的位置来手动绘制四分位数,然后用分组线或哑铃或类似的东西将它们分别叠加到香草小提琴上。 Plotly 应该能够显示线或点几何。但我认为这比不那么优雅的结果更麻烦。

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 2022-12-14
      • 1970-01-01
      • 2016-07-02
      • 2019-07-07
      • 1970-01-01
      • 2021-10-23
      • 2018-05-19
      • 2020-07-03
      相关资源
      最近更新 更多