如何在R中使用ggplot覆盖多个MATPLOT?

如何在R中使用ggplot覆盖多个MATPLOT?,r,ggplot2,R,Ggplot2,我想做的是;我有几个不同的矩阵。。。通常我可以使用matplot(并通过融合使用ggplot)绘制它们,但我试图实现的是将3个矩阵叠加在一个单独的绘图上 例如,如果我创建了3个不同的矩阵,如下所示: mat <- NULL for(i in 1:3){ set.seed(i) mat[i] <- list(matrix(rbinom(100,20,0.6), ncol=10)) } 但我想做的是将所有3个矩阵叠加在同一个图上(最好使用ggplot)。。。有没有关于我如何做

我想做的是;我有几个不同的矩阵。。。通常我可以使用
matplot
(并通过融合使用ggplot)绘制它们,但我试图实现的是将3个矩阵叠加在一个单独的绘图上

例如,如果我创建了3个不同的矩阵,如下所示:

mat <- NULL
for(i in 1:3){
  set.seed(i)
  mat[i] <-  list(matrix(rbinom(100,20,0.6), ncol=10))
}
但我想做的是将所有3个矩阵叠加在同一个图上(最好使用ggplot)。。。有没有关于我如何做到这一点的建议?

我们可以使用

library(ggplot2)
library(dplyr)
library(tidyr)
library(purrr)
map_dfr(mat, ~ as.data.frame(.x) %>% 
      mutate(id = row_number()), .id = 'grp') %>%
  pivot_longer(cols = -c(grp, id)) %>%
  ggplot(aes(x = id, y = value, group = grp, colour = name)) + 
     geom_line() + 
     theme_bw() +
     theme(legend.position = 'none')
我们可以使用

library(ggplot2)
library(dplyr)
library(tidyr)
library(purrr)
map_dfr(mat, ~ as.data.frame(.x) %>% 
      mutate(id = row_number()), .id = 'grp') %>%
  pivot_longer(cols = -c(grp, id)) %>%
  ggplot(aes(x = id, y = value, group = grp, colour = name)) + 
     geom_line() + 
     theme_bw() +
     theme(legend.position = 'none')

更新:见评论: 您可以绘制3个绘图,然后使用
patchwork
library进行排列

ibrary(ggplot2)
library(dplyr)
library(tidyr)
library(purrr)
df<- map_dfr(mat, ~ as.data.frame(.x) %>% 
          mutate(id = row_number()), .id = 'grp') %>% 
  pivot_longer(cols = -c(grp, id)) 


df1 <- df %>% 
  filter(grp==1)
df2 <- df %>% 
  filter(grp==2)
df3 <- df %>% 
  filter(grp==3)



p1 <- ggplot(df1, aes(x = id, y = value, colour = name)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = 'none')

p2 <- ggplot(df2, aes(x = id, y = value, colour = name)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = 'none')


p3 <- ggplot(df3, aes(x = id, y = value, colour = name)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = 'none')

library(patchwork)

p1 + p2 + p3 + plot_layout(ncol=1,heights=c(2,2,2))

更新:见评论: 您可以绘制3个绘图,然后使用
patchwork
library进行排列

ibrary(ggplot2)
library(dplyr)
library(tidyr)
library(purrr)
df<- map_dfr(mat, ~ as.data.frame(.x) %>% 
          mutate(id = row_number()), .id = 'grp') %>% 
  pivot_longer(cols = -c(grp, id)) 


df1 <- df %>% 
  filter(grp==1)
df2 <- df %>% 
  filter(grp==2)
df3 <- df %>% 
  filter(grp==3)



p1 <- ggplot(df1, aes(x = id, y = value, colour = name)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = 'none')

p2 <- ggplot(df2, aes(x = id, y = value, colour = name)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = 'none')


p3 <- ggplot(df3, aes(x = id, y = value, colour = name)) + 
  geom_line() + 
  theme_bw() + 
  theme(legend.position = 'none')

library(patchwork)

p1 + p2 + p3 + plot_layout(ncol=1,heights=c(2,2,2))


抱歉,我想我的问题还不够清楚。我将编辑它以反映我想要的。我想做的不是使用
facet\u wrap
绘制多个单独的图,而是将3个矩阵叠加到一个单独的图上plot@Electrino然后把“group=”grp“改一下,我刚才对另一个答案做了评论。。也许我错过了什么。。。但是使用你的方法不会产生相同的图。例如,如果我在上面的示例中绘制mat[[2]],它与中央面板(使用
facet\u wrap
时)不一样@Electrino您的问题是在同一个矩阵图中创建所有矩阵图,而此代码确实如此抱歉,我认为我的问题不够清楚。我将编辑它以反映我想要的。我想做的不是使用
facet\u wrap
绘制多个单独的图,而是将3个矩阵叠加到一个单独的图上plot@Electrino然后把“group=”grp“改一下,我刚才对另一个答案做了评论。。也许我错过了什么。。。但是使用你的方法不会产生相同的图。例如,如果我在上面的示例中绘制mat[[2]],它与中央面板(使用
facet\u wrap
时)显示不一样@Electrino您的问题是在同一个矩阵图中创建所有矩阵图,而此代码会导致这些图看起来不正确。。。例如,如果我使用上面的方法绘制mat[[2]]。。。它看起来不像这个
方面的中心图。。。我是不是遗漏了什么?对不起。我的错误。刚刚从
ggplot aes
中删除了
group=grp
。请查看我的编辑。如果我想避免使用
facet\u wrap
并将3个图重叠在一起?。当我删除
facet\u wrap
行时,绘图看起来又很奇怪了。请看我的编辑。对不起,我想我不清楚。。。我正在尝试将3个图叠加到一个图上。。。不是3个单独的情节。。。只有一个图,上面有3个矩阵。和另一个答案没什么不同。。。但另一个答案是,情节看起来很奇怪那些情节看起来不正确。。。例如,如果我使用上面的方法绘制mat[[2]]。。。它看起来不像这个
方面的中心图。。。我是不是遗漏了什么?对不起。我的错误。刚刚从
ggplot aes
中删除了
group=grp
。请查看我的编辑。如果我想避免使用
facet\u wrap
并将3个图重叠在一起?。当我删除
facet\u wrap
行时,绘图看起来又很奇怪了。请看我的编辑。对不起,我想我不清楚。。。我正在尝试将3个图叠加到一个图上。。。不是3个单独的情节。。。只有一个图,上面有3个矩阵。和另一个答案没什么不同。。。但另一个答案是,情节看起来很奇怪