Black Lives Matter. Please consider donating to Black Girls Code today.

Ggplotly renders different online vs offline

I am trying to embed ggplot style graphs on a webpage online. I use ggplot to create a graph, run it through ggplotly, and then paste the plotly_json into my page. I’ve found that the online version of plotly renders differently than the one in Rstudio. The one in Rstudio renders the ggplotly type correctly, but the online one does not render the axis labels.

Here is an example of creating a ggplotly. Note that it has y axis and x axis labels


p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)

# Divide by day, going horizontally and wrapping with 2 columns
p <- p + facet_wrap( ~ day, ncol=2)

p <- ggplotly(p)

When I render it online I don’t see the axis labels.

This seems to only be a problem when the ggplot includes a facet_wrap.

I know it is possible to do this correctly, because it renders properly at

I found that this is due to the margins. When ggplotly renders the image inside Rstudio, it chooses margins so that the x axis and y axis labels can show up. When this is posted online, the chosen margins are no longer applicable. Does anyone know how to dynamically set the margins?