2

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

enter image description here

# ggplotly object only shows first facet
ggplotly(pwrap)

enter image description here

# 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

nefosl
  • 366
  • 1
  • 8
  • Have you tried this answer https://stackoverflow.com/questions/46281534/ggplotly-removing-data-from-single-facet ? It seems like related. – Ronak Shah Mar 04 '21 at 06:45
  • Thanks for finding the related question. I tried a bit from that question and related links and still get gaps with my data. I haven't exhausted that info yet. – nefosl Mar 20 '21 at 02:32

2 Answers2

2

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)

enter image description here

nefosl
  • 366
  • 1
  • 8
1

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:

enter image description here

PKumar
  • 10,971
  • 6
  • 37
  • 52
  • This is interesting because it points out why my previous attempts were failing: the changing x value with each group. I don't understand why x indexes by 1 for each facet in the ggplotly object, even in your second version with a defined constant x value. I suppose I could define an x-axis transformation for each facet. – nefosl Mar 21 '21 at 12:45
  • I don't see anything about this in the [documentation] (https://plotly.com/ggplot2/box-plots/) – nefosl Mar 21 '21 at 12:46
  • Actually, combining the documentation and PKumar's suggestion led to a solution: x needs to be a factor. See solution. – nefosl Mar 21 '21 at 13:40