Plotly轴标签在多行对齐

时间:2017-09-12 08:22:19

标签: r shiny plotly

我正在使用一些Plotly水平条形图构建一个Shiny应用程序。有些数据标签很长,我想在多行上打破它们。当<br>位于我想要破坏线的位置时,它可以工作,但是轴标签的对齐并不顺利。请参见下面的输出(左)和所需的图(右)。

Plotly output (left) and desired output (right)

下面是一个最小的工作示例。它遗漏了Shiny部分,因为我希望这不会影响可能的解决方案。

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

非常感谢帮助!苦苦挣扎几个小时,但无法找到解决方案!

1 个答案:

答案 0 :(得分:1)

这不是很一般,但您可以使用tickvals调整刻度位置,例如:

ay <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = FALSE, 
tickvals = 0:9 + 0.25
)