简体   繁体   English

包裹长轴标签

[英]wrapping long axis labels

I'd like to have wrapped labels for categories. 我想要为类别包装标签。 Plotly is displaying spaces where I want line breaks. Plotly显示我想要换行的空格。 When the strings get too long it just displays them at a 45 degree angle. 当琴弦变得太长时,它只会以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")

I'm using Shiny / R 我正在使用Shiny / R.

I'd recommend wrapping the strings in the data frame first. 我建议首先在数据框中包装字符串。 So if your data frame is 所以,如果您的数据框是

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

Then wrap the strings with HTML line breaks at some sensible interval. 然后以一些合理的间隔用HTML换行符包装字符串。

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

And then use that column instead. 然后使用该列代替。 You may need to increase the margin at the bottom (in pixels). 您可能需要增加底部的边距(以像素为单位)。

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

In summary, \\n in strings are ignored in HTML so line breaks are <br> . 总之,在HTML中忽略字符串中的\\n ,因此换行符是<br>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM