我正在尝试设置一个for循环,该循环将为数据框中的每个基因创建框线图。循环的每个组件都在工作。但是,一旦将组件集成到循环中,我就不会得到任何结果。我不知道我的代码出了什么问题。你能帮我吗?
Merged[, 1] Apoa1 Fabp1 Aldob Reg1 Car4
1 Enterocyte 3052.41 4112.31 2342.12 7971.70 1519.83
2 Enterocyte 635.88 2790.60 3523.84 4160.18 1081.47
3 Enterocyte 46.35 702.58 1325.22 424.51 377.57
4 Enterocyte 293.36 900.89 1966.51 3688.59 644.94
5 Enterocyte 42.99 2921.10 800.14 1.94 1570.63
6 Enterocyte 0.00 784.43 1219.74 213.08 640.65
#(I don't have only enterocytes. This is just the first lines of the dataframe)
#To select the columns. Beginning at 1 because the first column is the cell type, not the TPM data
a=1
genenames <- unique(EC_relev$GeneName)
for (k in seq(1:length(genenames))){
i <- genenames[k] # I give the name of the gene to i
a <- a+1
# I create a dataframe for each gene containing the TPM data and the cell types
Dataf <- as.data.frame(Merged2[,1])
Dataf <- cbind(nam,Merged2[,3])
colnames(nam) <- c("CellType","TPM")
# I do the boxplot
ggplot(nam,aes(x=CellType,y=TPM))+
labs(title = paste(i),x="Cell Types",y="Transcript per Million")+
theme(axis.text.x.bottom = element_text(angle = 45,vjust = 1,hjust = 1,size = 10),
axis.text.y = element_text(size = 10),
legend.text = element_text(size=11,face = "bold"),
title = element_text(size = 13, face="bold"))+
geom_boxplot()
}
实际上,当我运行此命令时,没有任何结果或错误消息。所以我不知道发生了什么。
答案 0 :(得分:0)
当它们位于print
循环或for
s内时,必须明确地function
。
通过以下方式更改绘图:
g <- ggplot(nam,aes(x=CellType,y=TPM))+
labs(title = paste(i),x="Cell Types",y="Transcript per Million")+
theme(axis.text.x.bottom = element_text(angle = 45,vjust = 1,hjust = 1,size = 10),
axis.text.y = element_text(size = 10),
legend.text = element_text(size=11,face = "bold"),
title = element_text(size = 13, face="bold"))+
geom_boxplot()
print(g)