【发布时间】:2017-03-17 12:57:48
【问题描述】:
我有一个dataframe 如:
line station var
1 a 39446
1 b 82964
1 c 57840
1 d 78946
1 e 69972
1 f 14303
1 g 78179
2 a 37738
2 b 62261
2 c 19378
2 d 76435
2 e 17181
2 f 75148
2 g 10882
我想使用 plot_ly 从这些数据中创建一个子图。我想要一个带有 station 作为 x 值和 var 作为 y 值的条形图。我想有两个基于线的子图。我知道我可以这样做:
p1 <- plot_ly(data = df[df$line == "1", ], x = ~station, y = ~var, type = "bar")
p2 <- plot_ly(data = df[df$line == "2", ], x = ~station, y = ~var, type = "bar")
p3 <- subplot(p1, p2, nrows = 2)
这有点重复,因为p1 和p2 的代码基本相同。本地执行此操作的最快方法是什么?我知道facet_grid和ggplotly,但我想在剧情中原生地使用它。
谢谢你:)
【问题讨论】: