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).
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!