将 ggplotly 和 ggplot 与拼凑而成?

dss*_*dss 5 r ggplot2 plotly ggplotly patchwork

是否可以将 ggplot ly和 ggplot 与拼凑而成?

例子

这将并排显示两个图

library(ggplot2)
library(plotly)
library(patchwork)

a <- ggplot(data.frame(1), aes(X1)) + geom_bar()
b <- ggplot(data.frame(1), aes(X1)) + geom_bar()
a + b
Run Code Online (Sandbox Code Playgroud)

但是,如果我们将其转换为 ggplotly,则会出错

b  <- ggplotly(b)
a + b
Error: Can't add `b` to a ggplot object.
Run `rlang::last_error()` to see where the error occurred.
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个问题?

Dav*_*e2e 7

Plotly 具有subplot将多个图组合在一起的功能:

library(ggplot2)
library(plotly)

df1 <-data.frame(x=1:10, y=1:10)
df2 <- data.frame(x=10:1, y=1:10)

p1<-ggplot(df1, aes(x, y)) +geom_point()
fig1<-ggplotly(p1)

p2<-ggplot(df2, aes(x, y)) +geom_line()
fig2<-ggplotly(p2)

subplot(fig1, fig2, nrows=2)
Run Code Online (Sandbox Code Playgroud)

有关更多信息和示例,请参阅https://plotly.com/r/subplots/

  • 知道如何将两个图并排放置(而不是堆叠)吗?(我直观地使用了“ncols”,但这不是“subplot()”的有效参数)。编辑:这真的很简单,只需完全省略“nrows”参数,它就会自动并排。 (2认同)

小智 5

虽然有可能,但维护者表示这超出了当前计划

https://github.com/thomasp85/patchwork/issues/70