如何用R中的样本组制作特定行的箱线图

how to make boxplot of specific row with group of sample in R

我是R的新手,我想寻求帮助制作组箱线图我有2个文件,文件1是样本值(基因表达)test.txt

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

文件 2 是样本设计 design.txt

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

我想用一个特定的行在 R 中制作箱线图,例如:a1 并且有 2 个组 1 和 2;输出看起来像 boxplot-a1

如何直接从 2 个文件执行此操作?我觉得我做的很蠢

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

我通过复制和转置制作新文件:

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()

非常感谢您的帮助!

有了这样的数据,值得先将chr类型的变量转换为factor。

library(tidyverse)

df = read.table(
  header = TRUE,text="
  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") %>% 
  as_tibble() %>% 
  mutate(
    gene = gene %>% fct_inorder(),
    name = name %>% fct_inorder(),
    group = group %>% fct_inorder()
  )

现在您可以为 gene 变量的一个值制作箱线图

df %>% filter(gene == "a1") %>% 
  ggplot(aes(gene, expression))+
  geom_boxplot()

同时获得两个值

df %>%
  ggplot(aes(gene, expression, fill=gene))+
  geom_boxplot()