plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.54k stars 622 forks source link

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

Open thomas-neitmann opened 4 years ago

thomas-neitmann commented 4 years ago
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 commented 4 years ago

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.

dwlarson10 commented 2 years ago

Has there been any updated on a fix for this?