plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Improper alignment of boxplots in facets

Open ukkonen opened this issue 9 years ago • 5 comments

Hi, first of all, thanks for a great package! I have spent some time with it recently and I think that I have found a bug. If I use boxplots and facets, the boxplots in the upper facet are on the left side of the category while the boxplots in the bottom facet are on the right side of the category, while I believe that they should be aligned in the middle. Picture tells more than thousands words, so see bellow...

you can get it with the following code:

library(ggplot2)
library(plotly)

data.1 <- diamonds
data.1$set <- "set1"

data.2 <- data.1
data.2$set <- "set2"

my.data <- rbind(data.1, data.2)

p <- ggplot(my.data, aes(x=cut, y=price, fill= clarity))+
  geom_boxplot()+
  facet_grid(set ~ .)

ggplotly(p)%>%
  layout(boxmode = "group")

boxplots

ukkonen avatar Aug 29 '16 21:08 ukkonen

I am experiencing the same / similar problem with facet_wrap. If I plot a single facet, the plot looks great

p1 <- ggplot(diamonds[diamonds$color == "D",], aes(x=cut, y=price, fill= clarity))+
    geom_boxplot()+
    facet_wrap(~color, scales="free", ncol=1)
  
ggplotly(p1)%>%
    layout(boxmode = "group")

However, if I have several facets, the boxes become so thin they are imperceptible

p2 <- ggplot(diamonds, aes(x=cut, y=price, fill= clarity))+
    geom_boxplot()+
    facet_wrap(~color, scales="free", ncol=1)
  
ggplotly(p2, height=2000 )%>%
    layout(boxmode = "group")

Michelle-Muha avatar Dec 15 '16 18:12 Michelle-Muha

I have also encountered such issue with plotting boxplots with facets, either the boxes were stacked over each other, or they became too narrow:

Correct plot with ggplot:

g <- ggplot(ex, aes(x=Version, y=Weight, color=Lot))+
  geom_boxplot(outlier.shape = NA)+
  geom_point(position=position_jitterdodge(jitter.width=NULL, jitter.height=0, dodge.width=0.75)) +
  theme(axis.text.x = element_text(angle=45, hjust=1))+
  scale_y_continuous("Weight", breaks = pretty_breaks())+
  facet_wrap(~Model)
g

ggplot Plot the same thing with ggplotly (boxes of different group got stacked together): ggplotly Plot with ggplotly adding boxmode argument (boxes became too narrow):

ggplotly(g) %>% layout(boxmode = "group")

ggplotly2

qztseng avatar Feb 13 '17 04:02 qztseng

These are some different issues going on here:

  • https://github.com/ropensci/plotly/issues/697#issue-173873355 doesn't seem to be a problem anymore

  • https://github.com/ropensci/plotly/issues/697#issuecomment-267406661 and https://github.com/ropensci/plotly/issues/697#issuecomment-279292247 can be partially solved by making the plot wider and using layout.boxgroupgap.

Chalking this up as further evidence for https://github.com/ropensci/plotly/issues/706#issuecomment-292357273

cpsievert avatar Apr 20 '17 18:04 cpsievert

Hello, I am facing the same problem with python. See e.g. this picture: image

Is this solved yet? I cannot find any solution.

LukasHebing avatar Nov 30 '23 14:11 LukasHebing

I'm a little late to the game, but maybe this will help someone. I had a similar issue to @LukasHebing (single box per row/x-point) and found that using the parameter boxmode worked to get the category columns aligned.

I'm still not 100% sure why this happens, but I strongly suspect it's an implicit assumption about the data structure combination with color/facet_col/facet_row/x.

Example solution

Given the following data sample image

I would get the following results with the boxmode not specified:

# Plot figure (misaligned)
px.box(
    data,
    x='col',
    y='value',
    facet_row='row',
    color='group',
    # boxmode='overlay',
    width=500, height=300
)

image

And with the boxmode set to overlay:

# Plot figure (aligned)
px.box(
    data,
    x='col',
    y='value',
    facet_row='row',
    color='group',
    boxmode='overlay',
    width=500, height=300
)

image

However, if you have >1 box per category, this would cause the boxes to overlap (which is precisely what you set with that boxmode). Instead, using boxmode='group' should work.

sockchinglow avatar Mar 12 '24 15:03 sockchinglow