R中的子图

时间:2019-01-03 17:04:35

标签: r plotly subplot

我试图了解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图会导致其他迹线不显示。

谢谢你, 马可

0 个答案:

没有答案