As this comment indicates, plotly.js does "linebreak" horizontal legend items by default, but for some reason, it doesn't currently work when the legendgroup
attribute is used. Currently all ggplotly()
graphs with leverage this attribute (you can double-check that via plotly_json()
), but I think it's only really needed for faceted charts. Anyway, for this simple example, you could "fix" it this way:
ggplotly(plot) %>%
layout(legend = list(orientation = "h", xanchor = "center", x = 0.5, y = -0.1)) %>%
style(legendgroup = NULL)