plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

ggplotly() does not adjust margin when axis labels are wrapped

Open thomas-neitmann opened this issue 6 years ago • 2 comments

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

newplot (6)

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

newplot (7)

thomas-neitmann avatar Jan 08 '20 10:01 thomas-neitmann

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

newplot (10)

Interestingly, this works regardless of the value passed onto l.

thomas-neitmann avatar Jan 10 '20 13:01 thomas-neitmann

Has there been any updated on a fix for this?

dwlarson10 avatar Aug 22 '22 16:08 dwlarson10