gpt4 book ai didi

r - 将 ggplotly 和 ggplot 与拼凑而成?

转载 作者:行者123 更新时间:2023-12-04 00:57:07 24 4
gpt4 key购买 nike

是否可以结合使用 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

但是,如果我们将其转换为 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.

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

最佳答案

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/有关更多信息和示例。

关于r - 将 ggplotly 和 ggplot 与拼凑而成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61574401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com