r - Combine ggplotly and ggplot with patchwork?
问题描述
Is it possible to combine a ggplotly and a ggplot with patchwork?
Example
This displays the two plots side by side
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
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
将多个绘图组合在一起的功能:
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/。
推荐阅读
- javascript - 如何在 Node.js 中创建高效的追随者机制
- javascript - 如何处理 Nodejs 中的异步性?
- c++ - openCV C++ 中的 Vec3b 和 CV_8UC3 有什么区别?
- vb.net - 在文本文件中查找一行并将其显示在文本框中 VB.NET
- asp.net-core - 为什么我的实体主键在更新实体后显示被修改?
- mysql - 为什么 MySQL 5.7.17 在这个 JSON 列上给我一个语法错误?
- amazon-web-services - 如何使用 botocore 解密数据
- tmux - TMUX:将相关会话存储在一个组中?
- mysql - MYSQL - 使用 GROUP BY 时获取最新结果
- quartz-scheduler - Quartz:应用程序启动时作业未启动