包装长轴标签

wrapping long axis labels

我想要为类别包装标签。 Plotly 正在显示我想要换行符的空格。当字符串太长时,它只会以 45 度角显示它们。

 plot_ly(x =c("this\nand\nthat\nand\nall\nof\nthe\nthings",
 "the\nother\nstring\nthat\nmakes\nthis\nway\ntoo\nlong"), 
 y = c(1,2), name = "testing",type = "bar")

我正在使用闪亮/R

我建议先将字符串包装在数据框中。所以如果你的数据框是

df <- data.frame(x = c("this\nand\nthat\nand\nall\nof\nthe\nthings",
                       "the\nother\nstring\nthat\nmakes\nthis\nway\ntoo\nlong"), 
                 y = c(1, 2))

然后用 HTML 换行符以合理的间隔换行。

df$wrappedx <- sapply(df$x, 
                      FUN = function(x) {paste(strwrap(x, width = 16), collapse = "<br>")})

然后改用该列。您可能需要增加底部的边距(以像素为单位)。

plot_ly(data = df, 
        x = wrappedx,
        y = y,
        name = "testing",
        type = "bar") %>%
    layout(margin = list(b = 70))

总而言之,字符串中的 \n 在 HTML 中被忽略,所以换行符是 <br>.