r - For 循环不起作用,但循环的每个组件都有效
问题描述
我正在尝试设置一个 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()
}
实际上,当我运行它时,我没有得到任何结果或错误消息。所以我不知道发生了什么。
解决方案
print
当它们在for
循环或function
s内时,您必须明确地绘制图。
通过以下方式更改绘图:
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)
推荐阅读
- java - 任何实体的通用属性表
- amazon-web-services - 无法在 EC2 实例端口 8080 上确认 SNS 订阅的 HTTP 端点
- javascript - 提交前将上传附件名称添加到文本区域
- ios - iOS 14 中的 IAP 问题
- python - Scikit-feature CFS 在 python 上崩溃
- identityserver4 - 无法在 .netframework 4.61 API 中安装 IdentityServer3.AccessTokenValidation
- java - 如何在 google playStore 中更新过滤器 abi android?
- javascript - 铬的
因承诺拒绝而暂停 - java - 使用下面的代码嵌套的else if语句,不打印请帮助这个代码没有执行的原因是什么
- javascript - 如何在鼠标悬停时获取特定属性的值?