How to combine 2 charts in one picture using R studio?

Dear all,

I would like to join 2 charts with different color in one picture. Currently, I only can make them in separate pictures. Here is my data, data 1 and data 2
and here is my script

#data 1
data <- read.csv("data1.csv", header=T)

library(tidyverse)
library(ggplot2)

data %>%
  mutate(row = row_number()) %>%
  pivot_longer(cols = -row, values_drop_na = TRUE) %>%
  ggplot(aes(value)) + aes(row, value, color=name)  +
  geom_jitter()+ geom_hline(yintercept=1.4,linetype="dashed") + ylab("Log") + xlab("") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +  scale_color_manual(values = "purple") +
  theme(axis.ticks.x = element_blank(),axis.text.x = element_blank()) + theme(legend.title=element_blank()) +
  ggtitle("") + theme(axis.title.x = element_text(size = 15)) + 
  theme(axis.title.y = element_text(size = 15)) + 
  annotate(geom = "rect", xmin = 60, xmax = 300, 
           ymin = -Inf, ymax = Inf,
           color = "#D5D8DC", 
           fill = "#D5D8DC", alpha = 0.4
  )

#data 2
data <- read.csv("data2.csv", header=T)

library(tidyverse)
library(ggplot2)

data %>%
  mutate(row = row_number()) %>%
  pivot_longer(cols = -row, values_drop_na = TRUE) %>%
  ggplot(aes(value)) + aes(row, value, color=name)  +
  geom_jitter()+ geom_hline(yintercept=1.4,linetype="dashed") + ylab("Log") + xlab("") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +  scale_color_manual(values = "purple") +
  theme(axis.ticks.x = element_blank(),axis.text.x = element_blank()) + theme(legend.title=element_blank()) +
  ggtitle("") + theme(axis.title.x = element_text(size = 15)) + 
  theme(axis.title.y = element_text(size = 15)) + 
  annotate(geom = "rect", xmin = 60, xmax = 300, 
           ymin = -Inf, ymax = Inf,
           color = "#D5D8DC", 
           fill = "#D5D8DC", alpha = 0.4
  )

Does anyone know how to join 2 charts in one picture?
Please help me.
I appreciate your kindness.

Thank you!

I'm not sure if you want to just align the two plots or actually combine them. Are either of these useful, or have I not got the right end of the stick?

Align

If you want to align them, I'd reccomend using patchwork:

plot_data = function(df){
  
  df %>%
    mutate(row = row_number()) %>%
    pivot_longer(cols = -row, values_drop_na = TRUE) %>%
    ggplot(aes(value)) + aes(row, value, color=name)  +
    geom_jitter()+ geom_hline(yintercept=1.4,linetype="dashed") + ylab("Log") + xlab("") + 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
          panel.background = element_blank(), axis.line = element_line(colour = "black")) +  scale_color_manual(values = "purple") +
    theme(axis.ticks.x = element_blank(),axis.text.x = element_blank()) + theme(legend.title=element_blank()) +
    ggtitle("") + theme(axis.title.x = element_text(size = 15)) + 
    theme(axis.title.y = element_text(size = 15)) + 
    annotate(geom = "rect", xmin = 60, xmax = 300, 
             ymin = -Inf, ymax = Inf,
             color = "#D5D8DC", 
             fill = "#D5D8DC", alpha = 0.4
    )
  
}

plt1 = plot_data(data1)
plt2 = plot_data(data2)

patchwork::wrap_plots(plt1, plt2, guides = "collect")

Combine

To plot both datasets on the same plot, I'd probably look to bind them together first, then color by some additional column that is used to distinguish between them:

bind_rows(
  data1 %>% mutate(row = row_number()) %>% mutate(df = "A"),
  data2 %>% mutate(row = row_number()) %>% mutate(df = "B")
) %>%
  ggplot(aes(row, LOD, color = df)) + 
  geom_jitter()+ geom_hline(yintercept=1.4,linetype="dashed") + ylab("Log") + xlab("") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +  
  scale_color_manual(values = c("purple", "green")) +
  theme(axis.ticks.x = element_blank(),axis.text.x = element_blank()) + theme(legend.title=element_blank()) +
  ggtitle("") + theme(axis.title.x = element_text(size = 15)) + 
  theme(axis.title.y = element_text(size = 15)) + 
  annotate(geom = "rect", xmin = 60, xmax = 300, 
           ymin = -Inf, ymax = Inf,
           color = "#D5D8DC", 
           fill = "#D5D8DC", alpha = 0.4)

2 Likes

Thank you so much sir. It really what I want. Have a nice day!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.