包裹长轴标签

时间:2016-02-25 20:14:39

标签: r shiny plotly

我想要为类别打包标签。 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")

我正在使用Shiny / R

1 个答案:

答案 0 :(得分:4)

我建议首先将字符串包装在数据框中。因此,如果您的数据框是

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

总之,HTML中会忽略字符串中的\n,因此换行符为<br>

相关问题