将 ggplotly 和 ggplot 与拼凑在一起? [英] Combine ggplotly and ggplot with patchwork?

查看:11
本文介绍了将 ggplotly 和 ggplot 与拼凑在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 ggplotly 和 ggplot 拼凑在一起?

Is it possible to combine a ggplotly and a ggplot with patchwork?

这会并排显示两个图

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

但是如果我们将一个转换为 ggplotly,就会出错

But if we convert one to ggplotly, it errors

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

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

Is there a way to work around this?

推荐答案

Plotly 有 subplot 功能可以将多个绘图组合在一起:

Plotly has the subplot function to combine multiple plots together:

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)

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

See https://plotly.com/r/subplots/ for more information and examples.

这篇关于将 ggplotly 和 ggplot 与拼凑在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆