Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Plotly aspect ratio with small multiples / subplots

Tags:

plot

r

plotly

I've posted this as an issue on Github two weeks ago. Since it's apparently ok to repost the question "if the project maintainers don't respond in a reasonable amount of time", I'll post the question here.

I would like to create small multiples with a fixed aspect ratio. However, only some plots retain the correct aspect ratio. From what I gathered, setting a fixed aspect ratio with plotly is done as follows: layout(yaxis = list(scaleanchor = "x")). Consider the follwing example:

library(purrr)
library(plotly)

df <- data.frame(
  x = rep(1:5,25),
  y = rep(1:5,25),
  g = sort(rep(1:25,5))
)

plots <- df %>%
  split(.$g) %>%
  map(function(x){
    plot_ly(data = x, x = ~x, y = ~y, type = "scatter",mode = "lines") %>% add_lines()
  })

small_multiples <- subplot(plots,nrows = 5) %>%
  layout(yaxis = list(scaleanchor  = "x")) %>%
  hide_legend()

If I plot small_multiples, only the first plot (row 1, column 1) has an aspect ratio of 1. The other have an arbitrary aspect ratio. enter image description here Here's the link to the interactive version of this plot: https://plot.ly/~rata_zhaw/1/

Interestingly, if I choose the option shareX = T in subplot(), the entire first column has the correct aspect ratio. Nothing changes if I choose shareY = T enter image description here

And here's the link to the interactive version of the second plot: https://plot.ly/~rata_zhaw/3/

If I plot any of the plots individually, the aspect ratio is correct:

plots[[10]] %>%
  layout(yaxis = list(scaleanchor  = "x")) %>%
  hide_legend()
like image 925
Ratnanil Avatar asked Feb 17 '26 17:02

Ratnanil


1 Answers

What about

small_multiples <- subplot(plots,nrows = 5) %>%
  layout(scene = list(aspectration=list(x=1,y=1))) %>%
  hide_legend()

enter image description here

like image 95
Stéphane Laurent Avatar answered Feb 20 '26 06:02

Stéphane Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!