重叠的 R 子图

问题描述 投票:0回答:1

我正在尝试制作一个图,其中所有以下图均按行显示

fin_def<-subplot(fit_def, over_def,mov_def,pass_def,d_def) %>% layout(title = list(text = "Def Stats"))
fin_mid<-subplot(fit_mid, over_mid,mov_mid,pass_mid,d_mid)%>% layout(title = list(text = "Mid Stats"))
fin_fwd<-subplot(fit_fwd, over_fwd,mov_fwd,pass_fwd,d_fwd)%>% layout(title = list(text = "Fwd Stats"))
subplot(t_comp,fin_def,fin_mid,fin_fwd, nrows = 4, margin = 10)

但是它重叠了图而不是在不同的行中显示它们 overlapped plots

t_comp 是我在 ggplot2 中制作的流程图,并使用 ggplotly() 函数将其转换为绘图图,其他 3 个图是使用plotly 制作的组合雷达图。

plots <- list(t_comp, fin_def, fin_mid, fin_fwd)
plots <- lapply(plots, subplot)
subplot(plots, nrows = 4, margin = 100)

已经尝试增加边距并使用 lapply 制作子图 根本没用

r plotly visualization
1个回答
0
投票

供将来参考,如果您的问题可重现,您可能会更快地得到答案。

看起来您是新手;欢迎来到社区!如果您想快速获得满意的答案,最好让您的问题具有可重现性。这包括示例数据,例如

dput()
reprex::reprex()
的输出以及您正在使用的任何库。看看:制作 R 可重现的问题

至于你的答案......我可以看到一个放射状图,但我不确定你还在绘制什么。径向图、地图和其他一些 Plotly 类型需要特殊处理来绘制子图。 (受影响的图不使用传统的 x 和 y 轴。)

对于这些绘图类型,您必须指定域。如果您有多个绘图,则必须在布局函数中指定后续绘图将被调用的内容。

这个答案中的图表来自 Plotly 的网站(它们没有在他们的网站上组合,我只是使用数据)。

对于多个径向线,您不需要函数

subplot
,您需要参数
subplot
。检查一下:

library(plotly)

plot_ly(type = 'scatterpolar', r = c(0,1,2,2), theta = c(0,45,90,0),
        mode = 'markers') %>% 
  add_trace(type = 'scatterpolar', r = c(39, 28, 8, 7, 28, 39),
            theta = c('A','B','C', 'D', 'E', 'A'), fill = 'toself', 
            subplot = "polar2") %>%      # <--- the name for this plot in layout()
  layout(polar = list(domain = list(x = c(0, .45), y = c(0, 1))),  # domain for 1st radial
         polar2 = list(domain = list(x = c(.55, 1), y = c(0, 1)),  # domain for 2nd radial
                       radialaxis = list(visible = T, range = c(0, 50))))

如果您有更多绘图,可以将它们添加为轨迹。在

layout()
中,您必须为每个图添加域。您还可以修改已指定的绘图的域。

在此示例中,我重用了上一个示例中的代码,但我将其分配给

fig2
,而不是添加了另一个跟踪。您将在
layout()
中看到,我添加了散点图的域并修改了 2 个径向图的域。

# code from previous chunk assigned to fig2
fig2 <- plot_ly(type = 'scatterpolar', r = c(0,1,2,2), theta = c(0,45,90,0),
                mode = 'markers') %>% 
  add_trace(type = 'scatterpolar', r = c(39, 28, 8, 7, 28, 39),
            theta = c('A','B','C', 'D', 'E', 'A'), fill = 'toself', 
            subplot = "polar2") %>%      # <--- the name for this plot in layout()
  layout(polar = list(domain = list(x = c(0, .45), y = c(0, 1))),  # domain for 1st radial
         polar2 = list(domain = list(x = c(.55, 1), y = c(0, 1)),  # domain for 2nd radial
                       radialaxis = list(visible = T, range = c(0, 50))))

# added scatter trace
fig2 %>% add_trace(x = 1:4, y = 4:1, type = "scatter", mode = "markers") %>% 
  layout(yaxis = list(domain = c(0, .5)),                    # scatter plot yaxis domain
         polar = list(domain = list(x = c(0, .45), y = c(.55, 1))),  # 1st radial update
         polar2 = list(domain = list(x = c(.55, 1), y = c(.55, 1)))) # 2nd radial update

由于我没有必要的信息来准确重现您的代码和绘图的情况,这可能会导致新的问题。让我知道。

© www.soinside.com 2019 - 2024. All rights reserved.