2016-02-25 83 views
3

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

回答

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

综上所述,\n在字符串中的HTML被忽略,所以换行是<br>。 “

+0

”总而言之,\ n在字符串中被忽略,所以换行符是
“完美!已经做了一个字符串菜刀,所以刚刚在 – hedgedandlevered

+1


subbed边缘增加也有帮助!尽管我建议将它作为图中最长标签的函数,而不是常量。 通过一些实验,至少对于Courier New而言,等宽公式是'(5/4 * fontSize * max(linesOfText))'像素 – hedgedandlevered

+0

做一个边距尺寸函数的好主意。我一定会觉得有用。 – mal