Multiple replicates and ggplot
1
1
Entering edit mode
3.8 years ago
singhsk2622 ▴ 20

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 • 3.2k views
ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
3.8 years ago
4galaxy77 2.9k

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')
ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1490 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6