简体   繁体   中英

How to overlay multiple matplots using ggplot in R?

What Im trying to do is; I have a few different matrices... normally I could plot them using matplot (and by melting to use with ggplot) but what I'm trying to achieve is to overlay the 3 matrices on one single plot.

For example, if I have create 3 different matrices like so:

mat <- NULL
for(i in 1:3){
  set.seed(i)
  mat[i] <-  list(matrix(rbinom(100,20,0.6), ncol=10))
}

I could normally plot each matrix separately using matplot (and ggplot) like so:

library(ggplot2)
library(reshape)

# for example, plotting just the 2nd matrix using matplot
matplot(mat[[2]],  type = c("b"), pch=1, col = 1:10)

# Now ggplot
data <- as.data.frame(mat[[2]])
data <- as.data.frame(data)
data$id <- 1:nrow(data)
# reshape to long format
plot_data <- melt(data, id.var = "id")

ggplot(plot_data, aes(x = id, y = value, group = variable, colour = variable)) +
  geom_line() +
  theme_bw() +
  theme(legend.position = "none")

But what I would like to do is to overlay all 3 matrices on the same plot (preferably using ggplot)... Any suggestions as to how I could do this?

We can use

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')

Update: see comments: You could plot 3 plots and then arrange with 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))

在此处输入图像描述

Maybe you meant to facet_warp In essence this is the code of akrun. Just added facet_wrap and removed group = grp

ibrary(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, colour = name)) + 
  geom_line() + 
  facet_wrap(~ grp) +
  theme_bw() + 
  theme(legend.position = 'none')

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM