如何使用 ggplot 对基因表达数据框进行箱线图,仅子集化特定基因并将我的样本划分为 2 个条件
How to use ggplot to boxplot a gene expression dataframe subsetting only a specific gene and dividing my samples in 2 conditions
我是 R 的新手,我有一些与 bloxpot 相关的基本问题。我有一个数据框 9800 obs。 (这些是基因)17 个变量(这些是我的样本),以及这些基因的表达值。我在下面放了一个简化的例子。
View(df)
sample1 sample2 sample3
gene1 1 2 25
gene2 5.2 5 32
gene3 3.1 3 50
gene4 2.5 2.6 21
首先,我想将每个样本与特定条件(响应者或无响应者)相关联。在这种情况下,样本 1 和 2 将是响应者,而样本 3 将是非响应者。如果我像这样创建一个数据框,会有用吗?
condition <- c('responder','responder','non-responder')
sample_condition <- cbind(colnames(df), condition)
View(sample_condition)
sample condition
sample1 responder
sample2 responder
sample3 non-responder
现在,我想使用 ggplot 绘制一个箱线图,显示我设计的 2 个条件下 gene1 的表达值。 example of the boxplot I want. I would like to include the dots for each sample as well
我认为我的问题实际上是如何告诉 ggplot() 绘制 df 仅对特定基因进行子集化并将我的样本划分为我之前设计的 2 个条件。
提前致谢!
欢迎光临,
看起来您需要先将宽格式数据转换为长格式数据,然后再创建箱线图。我将在 tidyverse 中使用 Hadley Wickham 的几个软件包来实现这一目标。
library(tidyverse)
df <- gather(df, condition, values, -condition)
ggplot(df, aes(condition, values))+
geom_boxplot()
制作完底图后,您可以在顶部添加一些透明度或 alpha
和一些 "wiggle" 围绕垂直轴的点,以使它们显示得更好并带有一些颜色。
ggplot(df, aes(condition, values, color = condition))+
geom_boxplot(outler.fill = F, alpha = .5)+
geom_jitter(alpha = .5,width = .1)
您的示例数据
df <- data.frame(sample1=runif(4),
sample2=runif(4),
sample3=runif(4))
rownames(df) <- c("gene1","gene2","gene3","gene4")
sample1 sample2 sample3
gene1 0.7068424 0.81313273 0.1021884
gene2 0.2212768 0.87664923 0.3599538
gene3 0.7835704 0.08712978 0.7942733
gene4 0.3909335 0.70202803 0.8851641
定义您的响应者和非响应者
responders <- c("sample1","sample2")
nonresponders <- setdiff(colnames(df),responders)
仅过滤 gene 1
和标签条目
library(tidyverse)
gene1 <- df[1,] %>%
gather() %>%
mutate(category=ifelse(key%in%responders,"responder","nonresponder"))
制作情节
qplot(x=category, y=value, data=gene1, geom=c("boxplot","jitter"), fill=category)
我是 R 的新手,我有一些与 bloxpot 相关的基本问题。我有一个数据框 9800 obs。 (这些是基因)17 个变量(这些是我的样本),以及这些基因的表达值。我在下面放了一个简化的例子。
View(df)
sample1 sample2 sample3
gene1 1 2 25
gene2 5.2 5 32
gene3 3.1 3 50
gene4 2.5 2.6 21
首先,我想将每个样本与特定条件(响应者或无响应者)相关联。在这种情况下,样本 1 和 2 将是响应者,而样本 3 将是非响应者。如果我像这样创建一个数据框,会有用吗?
condition <- c('responder','responder','non-responder')
sample_condition <- cbind(colnames(df), condition)
View(sample_condition)
sample condition
sample1 responder
sample2 responder
sample3 non-responder
现在,我想使用 ggplot 绘制一个箱线图,显示我设计的 2 个条件下 gene1 的表达值。 example of the boxplot I want. I would like to include the dots for each sample as well
我认为我的问题实际上是如何告诉 ggplot() 绘制 df 仅对特定基因进行子集化并将我的样本划分为我之前设计的 2 个条件。
提前致谢!
欢迎光临, 看起来您需要先将宽格式数据转换为长格式数据,然后再创建箱线图。我将在 tidyverse 中使用 Hadley Wickham 的几个软件包来实现这一目标。
library(tidyverse)
df <- gather(df, condition, values, -condition)
ggplot(df, aes(condition, values))+
geom_boxplot()
制作完底图后,您可以在顶部添加一些透明度或 alpha
和一些 "wiggle" 围绕垂直轴的点,以使它们显示得更好并带有一些颜色。
ggplot(df, aes(condition, values, color = condition))+
geom_boxplot(outler.fill = F, alpha = .5)+
geom_jitter(alpha = .5,width = .1)
您的示例数据
df <- data.frame(sample1=runif(4),
sample2=runif(4),
sample3=runif(4))
rownames(df) <- c("gene1","gene2","gene3","gene4")
sample1 sample2 sample3
gene1 0.7068424 0.81313273 0.1021884
gene2 0.2212768 0.87664923 0.3599538
gene3 0.7835704 0.08712978 0.7942733
gene4 0.3909335 0.70202803 0.8851641
定义您的响应者和非响应者
responders <- c("sample1","sample2")
nonresponders <- setdiff(colnames(df),responders)
仅过滤 gene 1
和标签条目
library(tidyverse)
gene1 <- df[1,] %>%
gather() %>%
mutate(category=ifelse(key%in%responders,"responder","nonresponder"))
制作情节
qplot(x=category, y=value, data=gene1, geom=c("boxplot","jitter"), fill=category)