首页 > 解决方案 > R 中的子图与 Plotly

问题描述

我试图了解 Plotly 中的子图功能是如何工作的。

我有以下两个例子:

library(plotly)

## Create a data frame with data to be plotted
S <- seq(1,100)
DF <- data.frame(S = S, A = S, B = sin(S), C = S^2, D = 1/S, E = S^3 + S^3*cos(S))

## Create first plot, 'P_A' with trace 'A'

DF %>% 
plot_ly() %>% 
add_lines(x = ~S , y = ~ A, yaxis = "y", name = "A",color = I("red")) %>%
layout(yaxis = list(side = "left", title = "Axis A")) -> P_A


## Create second plot, 'P_BC', with traces 'B' and 'C'

DF %>% 
plot_ly() %>% 
add_lines(x = ~S , y = ~ B, yaxis = "y", name = "B", color = I("blue")) %>%
add_lines(x = ~S , y = ~ C, yaxis = "y2", name = "C", color = I("green")) %>%
layout(yaxis = list(side = "left", title = "Axis B"),
     yaxis2 = list(side = "right", title = "Axis C", overlaying = "y2")) -> P_BC


## Create third plot, 'P_DE', with traces 'D' and 'E'

DF %>% 
plot_ly() %>% 
add_lines(x = ~S , y = ~ D, yaxis = "y", name = "D", color = I("orange")) %>%
add_lines(x = ~S , y = ~ E, yaxis = "y4", name = "E", color = I("purple")) %>% 
layout(yaxis = list(side = "left", title = "Axis D"),
     yaxis4 = list(side = "right", title = "Axis E", overlaying = "y4")) -> P_DE

## Create a subplot
subplot(list(P_A,P_BC,P_DE),nrows = 2)

subplot(list(P_BC,P_DE),nrows = 2)

我不明白为什么在第二个子图中,排除 P_A 图会导致其他轨迹不显示。

谢谢你,马可

标签: rplotlysubplot

解决方案


推荐阅读