如何对plot_ly()图表进行分面?

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

使用

ggplot2
plotly
facet_wrap()
制作交互式散点图。

library(ggplot2)
library(plotly)

g<-iris%>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
  geom_point()+
  facet_wrap(vars(Species))
ggplotly(g)

是否可以使用

plot_ly()
函数进行“分面”?文档建议
subplot()
...

p<-iris%>%
  group_by(Species)%>%
  plot_ly(x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, type = "scatter")%>%
  subplot() ##Something else here? 
p

r r-plotly
1个回答
30
投票

1:使用

plot_ly
do()
方法对
subplot()
图表进行分面:

library(plotly)
iris%>%
  group_by(Species) %>%
  do(p=plot_ly(., x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, type = "scatter")) %>%
  subplot(nrows = 1, shareX = TRUE, shareY = TRUE)

2:使用新的

plot_ly
函数绘制
dplyr::group_map()
图表。

library(dplyr)
iris%>%
  group_by(Species) %>%
  group_map(~ plot_ly(data=., x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, type = "scatter", mode="markers"), .keep=TRUE) %>%
  subplot(nrows = 1, shareX = TRUE, shareY=TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.