首页 > 解决方案 > 如何刻面 plot_ly() 图表?

问题描述

使用ggplot2plotly制作交互式散点图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)

带有 facet_wrap 的 ggplot2 散点图,以 plotly 呈现

是否可以使用该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

如何刻面这个 plot_ly 散点图?

标签: rr-plotly

解决方案


1:使用and方法刻面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)

使用 group_by() 和 do() 函数制作的 Plot_ly 图表

plot_ly2:使用新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)

推荐阅读