2016-02-25 89 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