This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Multiple replicates and ggplot

Hi

I want to try ggplot to draw bar chart or line graph with 6 samples and each samples has 6 replicates. I am wondering how should I consider these replicates? should I take average or I should use reshape that

Here is an example:

sample_name R1  R2  R3  R4  R5  R6
C 3h    28.0    23.6    16.4    21.0    33.2    29.4
LPS 3h  18.5    21.8    28.9    30.6    25.5    34.2
PI 3h   23.0    15.4    16.6    15.7    54.3    27.1
C 6h    47.9    56.7    40.9    94.9    64.6    38.7
LPS 6h  29.3    33.2    18.1    19.8    22.9    52.3
PI 6h   36.6    35.7    37.3    34.5    54.3    65.9

best,
sachin

ggplot

with OP input data:

df=read.csv("test.txt", strip.white = T, header = T, sep="\t")
library(dplyr)
library(ggplot2)
library(plotrix)

df %>%
    separate(sample_name, c("Sample", "Time"), sep = " ") %>%
    group_by(Sample, Time) %>%
    mutate(avg = mean(c_across(starts_with("R"))),
           se=std.error(c_across(starts_with("R")))) %>%
    ggplot(aes(Sample,avg, fill=interaction(Sample,Time), group=Time)) +
    geom_bar(stat="identity", position = "dodge") +
    geom_errorbar(aes(ymin = avg-se, ymax = avg+se),
                  position = position_dodge(0.9), width = .3)

Rplot

Please change the aesthetics as per your requirements.

1 answer

Something like this?

dat = structure(list(V1 = c("C", "LPS", "PI", "C", "LPS", "PI"), sample_name = c("3h", 
"3h", "3h", "6h", "6h", "6h"), R1 = c(28, 18.5, 23, 47.9, 29.3, 
36.6), R2 = c(23.6, 21.8, 15.4, 56.7, 33.2, 35.7), R3 = c(16.4, 
28.9, 16.6, 40.9, 18.1, 37.3), R4 = c(21, 30.6, 15.7, 94.9, 19.8, 
34.5), R5 = c(33.2, 25.5, 54.3, 64.6, 22.9, 54.3), R6 = c(29.4, 
34.2, 27.1, 38.7, 52.3, 65.9)), row.names = c(NA, -6L), class = c("data.table", 
"data.frame"))

dat %>% 
    dplyr::mutate(sample_name = stringr::str_c(V1, sample_name, sep="_")) %>%
     dplyr::select(-V1) %>% 
     tidyr::pivot_longer(-sample_name) %>% 
     ggplot(aes(x=sample_name, y=value, fill=name)) + 
     geom_bar(stat='identity', position='dodge')

Hi Thanks! But i want to combine all replicates together and show error bar in that.

Log in to answer this question.