如何刻画plot_ly()图表?

时间:2019-09-25 16:52:47

标签: r r-plotly

使用ggplot2plotlyfacet_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)

ggplot2 scatterplot with facet_wrap, rendered in 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

how to facet this plot_ly scatterplot?

1 个答案:

答案 0 :(得分:1)

这有效,但感觉像是黑客。

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)

Plot_ly chart made with group_by() and do() function

这是格子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)