1

There are groups of boxplots:

dat <- data.frame(xval = sample(100,1000,replace = TRUE),
                  group_af = as.factor(sample(c("a","b","c", "e", "f"),1000,replace = TRUE)),
                  group_xyz = as.factor(sample(c("x","y","z"),1000,replace = TRUE)))
dat <- dat %>% 
  mutate(aux_var = as.factor(paste0(group_af, "-", group_xyz))) 
lvls <- levels(dat$aux_var)

dat %>%
  plot_ly() %>% 
  add_trace(x = ~as.numeric(aux_var), 
            y = ~xval, 
            color = ~group_xyz, 
            type = "box",
            hoverinfo = "none",
            boxpoints = FALSE) %>%
  layout(boxmode = "group") %>%
  add_markers(x = ~jitter(as.numeric(aux_var)), 
              y = ~xval, 
              color = ~group_xyz,
              marker = list(size = 3),
              hoverinfo = "text",
              text = ~paste0("Group: ", aux_var, "<br>xval: ", xval),
              showlegend = TRUE) %>%
  layout(
    xaxis = list(tickmode = "array", 
                 tickvals = c(1:length(lvls)),
                 # ticktext = lvls
                 ticktext = sapply(lvls, function(x) gsub("-.*$", "", x))
                 ))

enter image description here

and I would like to add spaces between groups (or add border to each group) to significantly separate those groups and improve readability:

enter image description here

Is it possible to do this? For example add custom spaces on X axis between specific ticks?

Taz
  • 5,755
  • 6
  • 26
  • 63

1 Answers1

1

You can use layout to add shapes(reference) which in your case is multiple lines:

library(plotly)

dat <- data.frame(xval = sample(100,1000,replace = TRUE),
                  group_af = as.factor(sample(c("a","b","c", "e", "f"),1000,replace = TRUE)),
                  group_xyz = as.factor(sample(c("x","y","z"),1000,replace = TRUE)))
dat <- dat %>% 
  mutate(aux_var = as.factor(paste0(group_af, "-", group_xyz))) 
lvls <- levels(dat$aux_var)

upd <- dat %>%
  plot_ly() %>% 
  add_trace(x = ~as.numeric(aux_var), 
            y = ~xval, 
            color = ~group_xyz, 
            type = "box",
            hoverinfo = "none",
            boxpoints = FALSE) %>%
  layout(boxmode = "group") %>%
  add_markers(x = ~jitter(as.numeric(aux_var)), 
              y = ~xval, 
              color = ~group_xyz,
              marker = list(size = 3),
              hoverinfo = "text",
              text = ~paste0("Group: ", aux_var, "<br>xval: ", xval),
              showlegend = TRUE) %>%
  layout(
    xaxis = list(tickmode = "array", 
                 tickvals = c(1:length(lvls)),
                 # ticktext = lvls
                 ticktext = sapply(lvls, function(x) gsub("-.*$", "", x))
    )) 

upd %>% layout(shapes = list(c(type='line', x0= 3.5, x1= 3.5, y0=0, y1=100, line=list(width=1)),
                             c(type='line', x0= 6.5, x1= 6.5, y0=0, y1=100, line=list(width=1)),
                             c(type='line', x0= 9.5, x1= 9.5, y0=0, y1=100, line=list(width=1))))

enter image description here

amrrs
  • 6,215
  • 2
  • 18
  • 27