Plotly flips ggplot2 boxplot

孤街浪徒 提交于 2020-02-25 06:10:35

问题


I want to plot boxplot using only summary statistics (INPUT is summary statistics for each ID).
plot1 is what I want, but when I convert it to a plotly object something goes wrong (ie., plotly flips boxplot).
However, if I plot boxplot the usual way (not using stat = "identity") everything works fine.

Question: Why plotly "flips" summarised ggplot2 boxplot and how to avoid this?

library(broom)
library(plotly)
library(tidyverse)

# Generate random data
# Calculate statistics
INPUT <- rnorm(100) %>%
    matrix(10) %>%
    apply(2, function(x) tidy(summary(x))) %>%
    bind_rows() %>%
    mutate(ID = letters[1:10])

# Plot boxplot using statistics
plot1 <- ggplot(INPUT, aes(ID)) +
    geom_boxplot(stat = "identity", aes(
        lower = q1,
        upper = q3,
        middle = median,
        ymin = minimum,
        ymax = maximum))

# Only ggplot2 produces right result
plot1; ggplotly(plot1)

# Plot boxplot usual way
plot2 <- INPUT %>%
    gather(variable, value, -ID) %>%
    ggplot(aes(ID, value)) +
        geom_boxplot()

# ggplot2 and plotly produces right result
plot2; ggplotly(plot2)

来源:https://stackoverflow.com/questions/46004079/plotly-flips-ggplot2-boxplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!