19

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: http://rpubs.com/dbouquin/180894

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.

3
  • I am not sure if this is what you are looking for, but you could add to your plotly plot: %>% layout(autosize = F, width = 1000, height = 600) and adjust the width and height of the plot as you wish. Commented May 15, 2016 at 18:43
  • Thanks for the suggestion, but that's not working out. Adjusting the layout that way just changes the size of the plotting space, but the headers stretch too. The graphs themselves stay nearly the same Commented May 15, 2016 at 20:31
  • Is scales = "free_y" what you're looking for? It's argument to the facet_wrap function, and ggplotly() will translate. Commented May 16, 2016 at 0:14

3 Answers 3

10

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}
library(plotly)
library(dplyr)

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

ggplotly(gg)
```

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

ggp_build
```

Looks like so:

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

In the current version of pltly for plotly_build the resultant object doesn't have a $layout$width or $layout$height property
8

Just like plot_ly(), ggplotly() has a height and a width argument, see ?ggplotly.

Generally speaking it is good practice to always set height/width. If you don't, and you share your plot with someone else, the sizing might be different depending on the context.

This is especially important for ggplotly(). At print time, in order to convert the sizing of things, it has to assume a height/width. If you don't specify these, it uses the size of your R graphics device, and we can't always guarantee the resize behavior is entirely consistent with what you'd get from your R graphics device.

Comments

8

I recommend you update to the latest version of plotly.

To see which version you're on: packageVersion("plotly")

You can then add the width and height parameters to your ggplotly() function calls.

el <- as.data.frame(economics_long[,1:3])
econp <- ggplot(el, aes(date, value, group=variable)) + 
  geom_line()+
  facet_grid(variable ~ ., scale = "free_y")+
  labs(title="US Economic time series")

ggplotly(econp, height = 350, width=600)

An extra suggestion I would add is that if you're running this in a R Markdown document / R Notebook, the figure itself may also need some adjustment so your plotly (which is really a htmlwidget) will not render with a scroller in an undersized window. This could be done with the fig.width and fig.height parameters:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.