Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper alignment of boxplots in facets #697

Open
ukkonen opened this issue Aug 29, 2016 · 5 comments
Open

Improper alignment of boxplots in facets #697

ukkonen opened this issue Aug 29, 2016 · 5 comments
Labels

Comments

@ukkonen
Copy link

ukkonen commented Aug 29, 2016

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

@Michelle-Muha
Copy link

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")

@qztseng
Copy link

qztseng commented Feb 13, 2017

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

@cpsievert
Copy link
Collaborator

cpsievert commented Apr 20, 2017

These are some different issues going on here:

Chalking this up as further evidence for #706 (comment)

@LukasHebing
Copy link

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

Is this solved yet? I cannot find any solution.

@sockchinglow
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants