R 子图大小不一致

标签 r plotly subplot

我正在尝试绘制一系列箱线图,但是子图会生成不同大小的图。我真的需要所有的图都具有相同的大小。

这是一些示例代码,我将在下面进行解释:

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'
    
  )

代码说明:

  • df1 和 df2 只是数据
  • create_plot() 函数只允许我创建相同的演示图,但关闭除我创建的第一个图之外的所有图上的图例
  • fig1 和 Fig2 是使用 create_plot() 函数创建的。图 1 显示了图例,图 2 没有显示图例
  • subplot 只是一遍又一遍地绘制相同的图,以证明即使图相同,它们最终的大小也不同

此代码为我生成以下输出:

The subplot

正如您所看到的,中间两行图的高度最低,最后一行图比任何其他行都大。您还将看到绘图标题最终出现在不同的位置,具体取决于行。当单独绘制时, plotly 结果很完美。

我已经尝试了子图中的各种设置,例如边距设置和高度设置,但它们似乎都非常特定于我想要的绘图大小和数字我的子图更加独立于渲染输出的大小和形状。

有什么想法吗?

最佳答案

我对边距有完全相同的问题,而不是标题(请参阅通过设置 yanchor =“bottom”修复这些问题的修改代码)。

我也玩过边距(不是高度),发现图的顶部和底部行看起来比中心行高(我的代码中有 5 行)。是否因为“中间”行的边距加在一起,因此绘图占用的空间较小?

我尝试平衡代码中的边距,但它不能解决问题。

将边距保留为默认值效果不佳,因为标题似乎与上面的图太接近。我不得不接受“最差”的显示选项,这不太好。

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?
) 

关于R 子图大小不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70750467/

相关文章:

r - 零索引的 Switch 语句

r - R 中的有效符号(标识符)由什么构成

python - DataTable - 单元格中的唯一颜色子字符串

matplotlib - x轴在pcolor和plot_date子图中不同

matlab - 如何链接 yyaxis 子图中的两个 y 轴?

r - 是否有为给定 R 版本下载包和依赖项的新解决方案

r - 将 libpq.5.dylib 复制到/usr/lib/libpq.5.dylib

image - 在保留标题和标签的同时删除子图中的死角

R从多列绘制x轴标签

python - plotly - 无法使用 bar_mpl() 在离线模式下绘制条形图