how to make box plot of specific row with group of sample in R

I am a new baby in R, I would like to ask for help to make the boxplot with the group I have 2 files, file 1 is the value of the samples (gene expression) test.txt

data

gene group1.1 group1.2 group2.1 group2.2
a1 12 13 12 12
a2 2 3 25 31
a3 24 30 34 22
a4 10 11 23 24

and file 2 is the sample design design.txt

design

file condition
group1.1 group1
group1.2 group1
group2.1 group2
group2.2 group2

I want to make the boxplot in R with one specific row for example: a1 and have 2 groups 1, and 2; the output looks like
boxplot-a1

How can I do this, direct from 2 files? I think I do the stupid way

dt1 <- read.delim("test.txt", sep="t", header = TRUE)
dg <- read.delim("design.txt", sep="t", header = TRUE)

I make the new file by copy and transpose:

enter image description here

gene name group expression
a1 Group1.1 group1 12
a1 Group1.2 group1 13
a1 Group2.1 group2 12
a1 Group2.2 group2 12.5
a2 Group1.1 group1 2
a2 Group1.2 group1 3
a2 Group2.1 group2 25
a2 Group2.2 group2 31

dt <- read.delim("test_t.csv", sep="t", header = TRUE)

a1 <- dt[dt$gene %in% "a1",]
ggplot(a1, aes(x=a1$group, y=a1$expression)) + 
    labs(title = "Expression A1", x = "Group", y = "Expression") +
    stat_boxplot(geom = "errorbar", width = 0.15) + 
    geom_boxplot().

and one more, if I have hundreds of genes (a1, a2, a3,a4…an), how can I use for loop to make all individual boxplots?
Thank you so much for your help!

Read more here: Source link