5

I am trying to create a bar plots with ggplotly and facet_wrap. However, the x-axis looks weird for the second and third subplots. below is my code:

library(plotly)
library(ggplot2)
library(dplyr)

setInfo<- data.frame(sampleNumber=rep(c("A","B","C"),4),
                     Category=rep(c("Class 1", "Class 2","Class 3"),4),
                     Value=rep(1,12))

setInfo %>% 
  ggplot(aes(sampleNumber,fill = sampleNumber))+
  geom_bar()+
  facet_wrap(~Category,nrow = 1,scales = "free_x")->plot_this

ggplotly(plot_this)
  • 3
    I think it's the same issue as this https://github.com/ropensci/plotly/issues/1221 – StupidWolf Mar 09 '20 at 18:45
  • Thanks so much! Seems to be the same problem – Qianshun Cheng Mar 09 '20 at 19:09
  • Following the link posted by @StupidWolf it seems to be a bug in (gg)plotly. Also, I tested with different random datasets. The specific issue seems to be related to the number of categories per panel. ggplotly with scales "free_x" breaks the axis in panels with only one category, except in the case that this happens to be the first panel, as in the example data provided by Qianshun. – stefan Mar 09 '20 at 19:32

1 Answers1

1

This issue is fixed via PR #1788.

As I figured out this issue was not related to the use of scales="free_x" but happened more generally when using a discrete axis with only one category present in panels > 1 (see my issue #1720 and also #1577).

I have had a look on the issue and was able to come up with a solution to fix this via PR #1788 which was merged on 2020-06-18.

After installing the latest devel version of plotly via devtools::install_github("ropensci/plotly") your example should now be rendered correctly like so:

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51