2

I have some data that looks nice when plotted with ggpglot but the x-axes and the plots underneath overlap in plotly::ggplotly().

library(gapminder)
library(plotly)
p <- ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) + geom_point() + scale_x_log10()
p <- p + aes(color=continent) + facet_wrap(~year,scale="free")
gp <- ggplotly(p)
gp

So I've been trying this: R: ggplot and plotly axis margin won't change

Basically I need to increase the white space between each graph object and the one below. Any thoughts would be much appreciated.

enter image description here

HCAI
  • 2,213
  • 8
  • 33
  • 65

1 Answers1

3

You can add vertical space in between your facets using:

theme(panel.spacing.y = unit(1, "line"))   # adjust to taste

However, there's a snag related to axis titles and facets when converting to plotly, where the axis title becomes an annotation and needs to be shifted more manually.

See these links for ways how:

https://stackoverflow.com/a/47228372/6851825

Converting ggplot object to plotly object creates axis title that overlaps tick values

https://github.com/ropensci/plotly/issues/1224

Jon Spring
  • 55,165
  • 4
  • 35
  • 53