[英]Wrap axis label when using ggplotly()
I am trying to create a horizontal bar chart using ggplotly()
.我正在尝试使用
ggplotly()
创建一个水平条形图。 Because the labels are rather long I inserted HTML line breaks <br>
.因为标签很长,我插入了 HTML 换行符
<br>
。 When plotting the data using ggplotly()
the label is indeed wrapped but there is big margin to the left of the label basically rendering the wrapping useless.当使用
ggplotly()
绘制数据时,标签确实被包装了,但标签左侧有很大的边距,基本上使包装无用。 Is there any way to fix this besides using plot_ly()
?除了使用
plot_ly()
之外,还有什么办法可以解决这个问题吗?
library(ggplot2)
library(plotly)
df <- data.frame(a = "A very long label<br>that needs<br>to be wrapped", b = 10)
ggplotly({
ggplot(df, aes(a, b)) +
geom_col() +
coord_flip()
})
plot_ly(df, y = ~a, x = ~b, type = "bar", orientation = "h")
Similarly to @asafpr's answer, adjusting the left margin using plotly::layout()
does the job:与@asafpr 的回答类似,使用
plotly::layout()
调整左边距可以完成这项工作:
library(ggplot2)
library(plotly)
df <- data.frame(a = "A very long label<br>that needs<br>to be wrapped", b = 10)
p <- ggplot(df, aes(a, b)) +
geom_col() +
coord_flip()
ggploty(p) %>%
layout(margin = list(l = 10))
Interestingly, the value passed onto l
does not seem to matter:有趣的是,传递给
l
的值似乎并不重要:
ggploty(p) %>%
layout(margin = list(l = 10))
ggploty(p) %>%
layout(margin = list(l = 1000))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.