【问题标题】:wrapping long axis labels包装长轴标签
【发布时间】:2016-02-25 20:14:38
【问题描述】:

我想为类别包装标签。 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

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

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

    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 中被忽略,所以换行符是&lt;br&gt;

    【讨论】:

    • "总之,字符串中的 \n 在 HTML 中被忽略,所以换行符是
      " 完美!已经做了一个切弦器,所以只需将
      替换为
    • 保证金增加也有帮助!尽管我建议将其作为图中最长标签的函数,而不是常数。通过一些实验,至少对于 Courier New,等宽,公式是 (5/4 * fontSize * max(linesOfText)) 像素
    • 创建边距大小函数的好主意。我肯定会觉得这很有用。
    猜你喜欢
    • 2014-02-02
    • 2014-03-19
    • 2012-10-10
    • 2018-06-08
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 2015-12-07
    • 2013-06-04
    相关资源
    最近更新 更多