Daina Daina - 1 year ago 89
R Question

Resize plotly R ggplotly

I'm having some trouble using the plotly R package. I'm very new to plotly but I loved that I could use ggplot-like syntax so I'm trying to make it work.

I created a faceted plot where you can hover over a datapoint and see details about that record. I'm very happy with the plot, but I'd like to resize it so the y-axis of each plot isn't so short, as in I'd like to adjust the height and width of the overall plot.

As is, I can't figure out how to override the default sizing and I'm pulling my hair out because all of the examples I can find use plot_ly() rather than ggplotly(). I'd rather not rebuild the plot just to adjust the sizing unless I need to.

The code I'm running currently is really simple:

plot <- ggplot(data = counts_country, aes(x = Year, y = Count, color = Region, text = paste("country:", Country))) +
geom_point(size= 2, alpha = (1/2)) +
facet_wrap(~ Region, ncol = 1)

(gg_plot <- ggplotly(plot))

You can see exactly what I'm working with here:

I tried adjusting the plot to show two rows of plots but still have trouble because the year labels get smashed together. Resizing seems like all I need.

Answer Source

Here's a workaround. Seems to work in R-Markdown documents which I am guessing is what you need? It still preserves the ggplot2 syntax but uses plotly_build() instead of ggplotly()

output: html_document

### Using ggplotly()
```{r, warning = F, message = F}

gg <- mtcars %>% 
  ggplot(aes(wt, mpg, color = gear)) + 
  geom_point() + 


### Using plotly_build()
ggp_build <- plotly_build(gg)
ggp_build$layout$height = 800
ggp_build$layout$width = 600


Looks like so:

enter image description here

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download