在R Plotly中相对于整个绘图区域居中轴标题

时间:2018-01-15 13:19:30

标签: r plotly

我在Plotly有一个水平条形图,有很长的轴标签和一个长的x轴标题。我在Shiny中使用这个数字,所以它的宽度取决于用户的屏幕sie。问题是在较小的屏幕上,轴标题被剪切。我希望它相对于整个绘图区域而不是轴居中,以防止这种情况。

最小例子:

data <- data.frame(
    x = c(0.5, 0.3, 0.1),
    y = c("Long axis label 1", "Long axis label 2", "Long axis label 3")
)

plot_ly(
    data,
    y = ~ y,
    x = ~ x,
    type = "bar"
) %>% layout(
        margin = list(l = 160, r = 20, b = 50, t = 20),
        xaxis = list(title = "Preeeeeeeeeeeeeety long X axis label", tickformat = "%"),
        yaxis = list(title = "Y axis label", tickprefix = "    ", tickwidth = 1, tickcolor = toRGB("white"))

    )

它在较小的屏幕上看起来如何:

enter image description here

以下是我希望如何:

enter image description here

我已经发现我可以通过添加像list(title = "Preeeeeeeeeeeeeety long X axis label&nbsp;&nbsp;&nbsp;")这样的非破坏空格来向左移动标题,但这不是我需要的。关于如何正确居中的任何想法?

1 个答案:

答案 0 :(得分:1)

这不是你想要的,但这是我所知道的唯一方式。希望它有所帮助:

library(dplyr)
library(plotly)

data <- data.frame(
  x = c(0.5, 0.3, 0.1),
  y = c("Long axis label 1", "Long axis label 2", "Long axis label 3")
)

plot_ly(
  data,
  y = ~ y,
  x = ~ x,
  type = "bar"
) %>% layout(
  margin = list(l = 160, r = 20, b = 50, t = 20),
  annotations = list(text = 'Preeeeeeeeeeeeeety long X axis label',
                     font = list(size = 12),
                     showarrow = F,
                     xref = 'paper', x = -0.3,
                     yref = 'paper', y = -0.2))

)

enter image description here