构建表达矩阵(瞎编的数据)
代码语言:javascript
复制set.seed(10010)
exp=matrix(rnorm(18),nrow=6)
exp=round(exp,2)
colnames(exp)=past0("gene",1:3)
rownames(exp)=past0("test",1:6)
转志
代码语言:javascript
复制library(tidyr)
library(tibble)
library(dplyr)
dat=t(exp)%>%
as.data.frame()
rownames_to_colomn() #ggplot2是不识别行名的,因此把行名变成一列
mutate(group = rep(c("control","treat"),each = 3)) #编数据,前三个是control 后三个是treat
宽变长
代码语言:javascript
复制pdat = dat%>%
pivot_longer(cols = starts_with("gene"), #或者pivot_longer(cols = 2:4,
names_to = "gene", #新建基因名列起名为“gene”
values_to = "count") #新建数据列起名为“count”
ggplot2绘图
代码语言:javascript
复制library(ggplot2)
p = ggplot(pdat,aes(gene,count))
geom_boxplot(aes(fill = group))
theme_bw()
p facet_wrap(~gene,scales = "free") #将图片分开为gene1,gene2,gene3三张图
导出