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