4

I'm building a Shiny application with some Plotly horizontal bar charts. Some data labels are pretty long and I want to break them on multiple lines. It works when places <br> on the place I want to break the line, but alignment of the axis labels does not go well. See picture below of output (left) and desired plot (right).

Plotly output (left) and desired output (right)

Below a minimal working example. It left out the Shiny part, because I expect that this does not affect the possible solution.

df <- data.frame(
  name = paste0('This is a pretty long sentence',1:10),
  dimA = 1:10
)

df$name <- gsub('This is a pretty long sentence','This is a<br>pretty long<br>sentence',df$name)


## Hide axes
ax <- list(
  title = "",
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE,
  domain = list(0.2, 1)
)
ay <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = TRUE,
  showgrid = FALSE
)

p <- df %>% 
  plot_ly(x = ~dimA, 
          y = ~name,
          type = 'bar',
          orientation = 'h'
  ) %>%
  layout(xaxis = ax, yaxis = ay)
p

Help is highly appreciated! Struggling with this for hours, but unable to find a solution!

Dendrobates
  • 3,294
  • 6
  • 37
  • 56

1 Answers1

1

This is not very general but you can adjust the tick position using tickvals, for example:

ay <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = FALSE, 
tickvals = 0:9 + 0.25
)
KGee
  • 771
  • 7
  • 26
  • Works fine for the problem above! You can of course dynamically build a wrapper around it, counting the number of breaks and constructing the tickvalues dynamically. Thx KGee for pointing in the right direction. – Dendrobates Sep 12 '17 at 10:58