plotly.R
plotly.R copied to clipboard
ggplotly() does not adjust margin when axis labels are wrapped
library(ggplot2)
library(plotly)
df <- data.frame(
a = "A very long label that needs to be wrapped",
b = 10,
c = "A very long label<br>that needs<br>to be wrapped"
)
wrap <- function(x) stringr::str_wrap(x, 10)
ggplotly({
ggplot(df, aes(a, b)) +
geom_col() +
coord_flip() +
scale_x_discrete(labels = wrap)
})

ggplotly({
ggplot(df, aes(c, b)) +
geom_col() +
coord_flip() +
scale_x_discrete(labels = wrap)
})

I found a workaround for this issue:
ggplotly({
ggplot(df, aes(a, b)) +
geom_col() +
coord_flip() +
scale_x_discrete(labels = wrap)
}) %>% layout(margin = list(l = 0))

Interestingly, this works regardless of the value passed onto l.
Has there been any updated on a fix for this?