问题
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