Reputation: 416
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
Upvotes: 2
Views: 1652
Reputation: 416
PKumar's solution is close (defining x variable), but the x axis was still not behaving as I expected. The solution is to make sure the x variable is a factor.
# plot using facet_wrap
pwrap <- ggplot(df, aes(x = as.factor(A), y = C)) + # x is a factor (as.factor('X') also OK if trying to match PKumar's answer)
geom_boxplot() +
theme_classic() +
facet_wrap(~A, scales='free')
# ggplot object
pwrap
# now a functional
ggplotly(pwrap)
Upvotes: 2
Reputation: 11128
Its actually coming but you are unable to see it, If you zoom out
you fill find all the plots, you can modify your code like below to make it work, Here I have changed the width to mean of the column mean(df$C)
.
On a side note you can make it better by using scales='free'
in the facet_wrap(facets = 'A', scales='free')
command, but in that case you have three vertical axis.
pwrap <- ggplot(df, aes(y = C)) +
geom_boxplot(width=mean(abs(df$C))) +
theme_classic() +
facet_wrap(facets = 'A')
# ggplot object shows fine
pwrap
ggplotly(pwrap)
Another version (scaled version), using x = 1 and scales='free' and without using the width option, The difference in both the plots is that the second plot is centered(plot is not attached), Please let me know if it helped:
# plot using facet_wrap
pwrap <- ggplot(df, aes(x = 'X', y = C)) +
geom_boxplot() +
theme_classic() +
facet_wrap(~A, scales='free')
# ggplot object shows fine
pwrap
ggplotly(pwrap)
Output from first version:
Upvotes: 1