简体   繁体   English

R Plotly 子图大小不一致

[英]R Plotly inconsistent subplot size

I'm trying to plot an array of box plots however subplot produces plots of different sizes.我正在尝试 plot 一个箱形图数组,但是子图会产生不同大小的图。 I really need all the plots to be the same size.我真的需要所有地块的大小相同。

Here is some example code which I'll explain a bit below:这是一些示例代码,我将在下面解释一下:

library(plotly)

df1 <- data.frame(label = sample(c("a", "b", "c","d", "e", "f"), 1000, replace = TRUE),
                 value = rnorm(1000,1))

df2 <- data.frame(label = sample(c("a", "b", "c", "d", "e", "f"), 1000, replace = TRUE),
                  value = rnorm(1000,2))


create_plot <- function(showlegend = F) {
  plot_ly( type = "box",
           showlegend = showlegend
           ) %>%
  add_trace(data = df1,
            x = ~label,
            y = ~value,
            offsetgroup = 0,
            legendgroup = 0,
            name = "Type 1") %>%
  add_trace(data = df2,
            x = ~label,
            y = ~value,
            offsetgroup = 1,
            legendgroup = 1,
            name = "Type 2") %>%
  layout(plot,
         boxmode = "group",
         annotations = list(
           x = 0.5,
           y = 1.05,
           text = "Plot Title",
           xref = "paper",
           yref = "paper",
           xanchor = "center",  # center of text
           yanchor = "center",  # center of text
          showarrow = FALSE
         )
  )
}

fig1 <- create_plot(showlegend = T)
fig2 <- create_plot()
    
#subplot(fig, fig1, fig1, fig1, fig1,
subplot(fig1, fig2, fig2, fig2, fig2, fig2, fig2, fig2,
        nrows = 4,
        # heights = c(0.2, 0.3, 0.3, 0.2),
        margin = c(0.07, 0.0, 0.05, 0.1) # c(left, right, top, bottom )
        ) %>%
  layout(
    bargroupgap = 0.01,
    plot_bgcolor='#e5ecf6'
    
  )

Code explanation:代码说明:

  • df1 and df2 is just the data df1 和 df2 只是数据
  • The create_plot() function just allows me to create the same demo plot but turn off the legend on all the but the first plot I create create_plot() function 只允许我创建相同的演示 plot 但关闭除我创建的第一个 plot 之外的所有图例
  • fig1 and fig2 are created using the create_plot() function. fig1 和 fig2 是使用 create_plot() function 创建的。 fig1 has the legend shown, fig2 does not图1显示了图例,图2没有
  • subplot just plots the same plot over and over to demonstrate that even though the plots are identical, they end up different sizes子图只是一遍又一遍地绘制相同的 plot 以证明即使图相同,它们最终的大小也不同

This code produces the following output for me:此代码为我生成以下 output:

次要情节

As you can see the middle two rows of plots have the lowest height and the last row plots are larger than any of the other rows.如您所见,中间两行图的高度最低,最后一行图比其他任何行都大。 You will also see that the plot titles end up in different locations depending on the row.您还将看到 plot 标题最终位于不同的位置,具体取决于行。 When plotted individually, the plot turns out perfectly.单独绘制时,plot 结果完美。

I have tried all sorts of settings in subplot like the margin settings and height settings but they all seem to be very plot size and number specific where as I would like my subplot to be more independent of the size and shape of the rendered output.我在子图中尝试了各种设置,例如边距设置和高度设置,但它们似乎都非常 plot 大小和数字,因为我希望我的子图更独立于渲染的 output 的大小和形状。

Any thoughts?有什么想法吗?

I'm having exactly the same issue with the margins, not the titles (see modified code which fixes them, by setting yanchor = "bottom").我在边距方面遇到了完全相同的问题,而不是标题(请参阅通过设置 yanchor =“bottom”来修复它们的修改代码)。

I too have played around with the margins (not so much the heights) and find that the top and bottom row of plots look taller than the centre rows (I have 5 in my code).我也玩过边距(不是高度),发现顶行和底行的图看起来比中心行高(我的代码中有 5 个)。 Is it because the margins are added together for the 'middle' rows and so the plots have less space to occupy?是因为“中间”行的边距被加在一起,所以地块占用的空间更少?

I've tried balancing the margins in your code but it doesn't fix the problem.我试过平衡代码中的边距,但它不能解决问题。

Leaving the margins as the default doesn't work well because the titles then seem to be too close to the plot above.将边距保留为默认值效果不佳,因为标题似乎与上面的 plot 太接近了。 I'm having to settle for the 'least worst' display option, which isn't great.我不得不满足于“最差”的显示选项,这不是很好。

library(plotly)

df1 <- data.frame(label = sample(c("a", "b", "c","d", "e", "f"), 1000, replace = TRUE),
                  value = rnorm(1000,1))

df2 <- data.frame(label = sample(c("a", "b", "c", "d", "e", "f"), 1000, replace = TRUE),
                  value = rnorm(1000,2))    

create_plot <- function(showlegend = F) {
  plot_ly( type = "box",
           showlegend = showlegend
  ) %>%
    add_trace(data = df1,
              x = ~label,
              y = ~value,
              offsetgroup = 0,
              legendgroup = 0,
              name = "Type 1") %>%
    add_trace(data = df2,
              x = ~label,
              y = ~value,
              offsetgroup = 1,
              legendgroup = 1,
              name = "Type 2") %>% 
    layout(boxmode = "group",
           bargroupgap = 0.01,
           plot_bgcolor='#e5ecf6',
           annotations = list(
             x = 0.5,
             y = 1.05,
             text = "Plot Title",
             xref = "paper",
             yref = "paper",
             xanchor = "center",  # center of text
             yanchor = "bottom",  # bottom of text
             showarrow = FALSE
           )
    )
}    

fig1 <- create_plot(showlegend = T)
fig2 <- create_plot()

#subplot(fig, fig1, fig1, fig1, fig1,
subplot(fig1, fig2, fig2, fig2, fig2, fig2, fig2, fig2,
        nrows = 4,
        # heights = c(0.2, 0.3, 0.3, 0.2),
        margin = c(0.07, 0.0, 0.05, 0.01) # c(left, right, top, bottom ) least worst option?
) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM