Home > Enterprise >  Overlaying two lines with confidence interval on the same graph
Overlaying two lines with confidence interval on the same graph

Time:10-10

I have plotted a line plot with confidence interval as below: enter image description here

Here is the code I used:

Data_prob = read.table("group1.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)

p<-ggplot(Data_prob, aes(x=Trial, y=Data))   theme_bw()   xlim(1, 49)   ylim(0.3, .95)   
  
  theme(plot.title = element_text(color="black", size=14, face="bold.italic"), # plot title
        axis.title.x = element_text(color="Black", size=25), # x label
        axis.title.y = element_text(color="Black", size=25), # y label
        axis.text.x = element_text(color = "black",face = "bold", # text style for x axis (removed face = "bold")
                                   size = 22, angle = 0),
        axis.text.y = element_text( color = "black",face = "bold", # text style for y axis
                                   size = 22, angle = 0),
        axis.line = element_line(color = "black", # line for x and y axis
                                 size = 1, linetype = "solid"),
        plot.caption = element_text(color = "black", size =11, face = "bold.italic"), # text style for caption
        axis.ticks.length=unit(.25, "cm")
        #axis.ticks = element_blank()
  )        
  theme(panel.border = element_blank(), # remove grid and background color
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())  

  geom_ribbon(aes(ymin=Data-CI, ymax=Data CI),
              alpha=0.2, fill = "gold",color = "gray35", size = 0.1)   
  geom_line(size=2, color="black") 
print(p)

# Change axis labels
p <- p   labs(x = "Trial", y = "Go probability")
p

Here is what the data look like:

structure(list(Trial = 1:20, Data = c(0.500000027777778, 0.529220307827301, 
0.519090892380421, 0.532167908861151, 0.598987738691911, 0.631452312399153, 
0.669892859606973, 0.649994605133695, 0.675688232680895, 0.708304011267941, 
0.720449809771278, 0.748081634160322, 0.761387966998141, 0.784275979741743, 
0.786897508201414, 0.794196638795235, 0.785998558171792, 0.766138054176142, 
0.790409435001615, 0.778745578955544), CI = c(5.44644605948509e-08, 
0.073455696656296, 0.080875149623623, 0.073926617913334, 0.085753364225061, 
0.068666346495005, 0.079656617789649, 0.077652237751934, 0.070180261163008, 
0.071432599780653, 0.064943477844168, 0.064645277387821, 0.065096219183598, 
0.065766579701286, 0.064325292909355, 0.066878706963396, 0.067698913200129, 
0.07105300711211, 0.067063389995005, 0.069931673037628)), row.names = c(NA, 
20L), class = "data.frame")

I would like to plot data from another group (group2.csv), so basically another line with confidence interval on the same graph to visually compare the two groups, ideally with a different line color. I've seen some examples on here but I couldn't get it to work. Could anyone help? Thank you!

CodePudding user response:

Suppose you read your data up to two tibble from csv files.

library(tidyverse)

Data_prob1 =tibble(
  Trial = 1:50,
  Data = c(seq(0.5,0.8, length.out = 20), rep(0.8, 30)) rnorm(50,0,0.05),
  CI = 0.1,
)

Data_prob2 =tibble(
  Trial = 1:50,
  Data = c(seq(0.8,1.2, length.out = 25), rep(1.2, 25)) rnorm(50,0,0.05),
  CI = 0.08
)

You can combine such data like this

Data_prob = Data_prob1 %>% mutate(probe = "1") %>% 
  bind_rows(Data_prob2 %>% mutate(probe = "2")) %>% 
  mutate(probe = probe %>% fct_inorder())

Finally, create a chart

Data_prob %>% ggplot(aes(Trial, Data, fill=probe, color = probe)) 
  geom_line() 
  geom_ribbon(aes(ymin=Data-CI, ymax=Data CI, color=NULL), alpha=0.2)

enter image description here

  • Related