I am plotting boxplots for multiple groups using ggplot() + geom_boxplot() + facet_wrap()
. I would like to use ggplotly()
on this object to use the hover tooltips so that I can quickly review data associated with the outliers. (Why am I not just annotating on the plot? I will add custom text to the tooltip that is more extensive than I would like to appear in a fixed annotation on the plot.)
When I use ggplotly
, the facets are displayed but only the data of the first facet appears. The same is true with facet_grid
. (I would prefer to use facet_wrap
so that I can use free scales between different facets.)
Is there a way to get ggplotly
to work with facets this way? I don't see anything in the ggplotly documentation. I've seen other examples where people have other issues with their facets but all the facets are there.
# make data:
# B: normal distribution divided across 3 groups
# C: group Z is on a different scale
df <- data.frame(A = rep(LETTERS[seq( from = 24, to = 26 )], 26*5),
B = rnorm(26*15))
df <- df %>% mutate(C = ifelse(A == 'Z', B*7,
ifelse(A == 'Y', B + 7, B)))
# plot using facet_wrap
pwrap <- ggplot(df, aes(y = C)) +
geom_boxplot() +
theme_classic() +
facet_wrap(facets = 'A')
# ggplot object shows fine
pwrap
# ggplotly object only shows first facet
ggplotly(pwrap)
# plot using facet_grid
pgrid <- ggplot(df, aes(y = C)) +
geom_boxplot() +
theme_classic() +
facet_grid(.~A)
# again, ggplot object shows fine
pgrid
# but ggplotly object is limited to first facet
ggplotly(pgrid)
# my goal: facet_wrap to allow 'free' y-scales for variable C, with ggplotly hover info
ggplot(df, aes(y = C)) +
geom_boxplot() +
theme_classic() +
facet_wrap(~A, scales = 'free')
Session info:
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
ggExtra_0.9
plotly_4.9.3
ggplot2_3.3.3