Grouped boxplot data not centred on xaxis ticks, looses boxgap argument, and wont resize with subplots
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 2.7k
- Forks
- 641
- PR merge metrics
- No merged PRs in 30d
Description
Hi @cpsievert!
Here after your Shiny webinar, great stuff!
I'm having issues with the boxplot data aligning with x-axis tick marks in subplots. Please see below:
library (data.table)
library (stringr)
library (plotly)
# some setup:
get_plot_title = function(this.plot.title, y.pos = 1, y.shift = 0){
list(text = sprintf ("<b>%s</b>", this.plot.title), yshift = y.shift
, xref = "paper", yref = "paper", yanchor = "center", xanchor = "center", align = "center"
, x = 0.5, y = y.pos, showarrow = FALSE, font = list (size = 16, family = "Arial")
)}
generate_panel = function(dat, trace.name){
my.plot =
plot_ly(dat, type = "box"
, x= ~get(x_var), y= ~get(y_var), color = ~get(color_var)
, whiskerwidth = 0.1
, boxpoints = F
) %>%
layout(boxmode = "group", boxgroupgap = 0.1, boxgap = 0.15
, plot_bgcolor = "#F5F5F5"
, annotations = get_plot_title(trace.name, y.shift = 20)
, xaxis = list(title = "", tickfont = list(size = 14, family = "Arial")
, tickmode = "linear", ticks = "outside")
, yaxis = list(title = sprintf("<b>%s</b>", y_var), titlefont = list (size = 16, family = "Arial"))
)
my.plot #return
}
x_var = "cut"
y_var = "price"
color_var = "color"
X_panel_var = "clarity"
test.dat = ggplot2::diamonds %>% setDT
Without subplots, everything looks great:
generate_panel(test.dat, trace.name = "all")

Adding two horizontal subplots:
test.dat[, get(X_panel_var) %>% levels][1:2] %>%
lapply(function(this.panel.var){
generate_panel(dat = test.dat[this.panel.var, on = X_panel_var], trace.name = this.panel.var)
}) %>%
subplot(shareY = T, shareX = F, titleX = F, nrows = 1, which_layout = 1, margin = 0.01)
Boxplot groups shift off the centre of the xaxis ticks, and the boxgap argument becomes irrelevant:

Enlarging this plot does not work at all (cuts off the data at the original subplot width and height, yikes!):

Interestingly, with 3 horizontal subplots, the middle plot data is centered around ticks, but the outer plots are even further away (and the boxgaps are worse):
test.dat[, get(X_panel_var) %>% levels][1:3] %>%
lapply(function(this.panel.var){
generate_panel(dat = test.dat[this.panel.var, on = X_panel_var], trace.name = this.panel.var)
}) %>%
subplot(shareY = T, shareX = F, titleX = F, nrows = 1, which_layout = 1, margin = 0.01)

Just wondering if you have any ideas / suggestions?
Thank you!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the supplied R diamonds example using generate_panel and subplot with two and three horizontal panels. Investigate the subplot boxplot layout and resizing entry points first. Done means grouped boxes remain centred on x-axis ticks, boxgap is respected, and enlarged subplots display all data.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- plotly, r
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100