![](/img/trans.png)
[英]How to prevent the y-axis from squishing the labels when using facet_wrap in ggplot/ggplotly?
[英]Wrap axis label when using ggplotly()
我正在嘗試使用ggplotly()
創建一個水平條形圖。 因為標簽很長,我插入了 HTML 換行符<br>
。 當使用ggplotly()
繪制數據時,標簽確實被包裝了,但標簽左側有很大的邊距,基本上使包裝無用。 除了使用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")
與@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))
有趣的是,傳遞給l
的值似乎並不重要:
ggploty(p) %>%
layout(margin = list(l = 10))
ggploty(p) %>%
layout(margin = list(l = 1000))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.