循环以将代码应用于不同的数据,并将图保存在R中

时间:2019-04-07 19:24:42

标签: r function for-loop pdf plot

我有一个数据帧df,如下所示:

df <- structure(list(times = c(724L, 1624L, 1569L, 2532L, 1271L, 2442L, 
757L, 848L, 3675L, 1229L, 1582L, 1257L, 1270L, 555L, 357L, 1133L, 
633L), Samples = structure(c(1L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), .Label = c("Sample1", 
"Sample10", "Sample11", "Sample12", "Sample13", "Sample14", "Sample15", 
"Sample16", "Sample17", "Sample2", "Sample3", "Sample4", "Sample5", 
"Sample6", "Sample7", "Sample8", "Sample9"), class = "factor"), 
    vital_status = c(1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 
    0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L), years = c(1.983561644, 
    4.449315068, 4.298630137, 6.936986301, 3.482191781, 6.690410959, 
    2.073972603, 2.323287671, 10.06849315, 3.367123288, 4.334246575, 
    3.443835616, 3.479452055, 1.520547945, 0.978082192, 3.104109589, 
    1.734246575), Gene1 = c(0.9, 0.8, 0.6, 1.2, 3.8, 2.3, 3.8, 
    0.4, 0.5, 1.2, 7.7, 2.1, 0.8, 1.8, 2.4, 3, 0.6), Gene2 = c(1.2, 
    3.8, 2.3, 3.8, 0.4, 0.5, 1.2, 7.7, 2.1, 0.9, 0.8, 0.6, 0.5, 
    1.2, 7.7, 2.1, 0.6), Gene3 = c(2.3, 3.8, 0.4, 0.5, 1.2, 7.7, 
    0.9, 0.8, 0.6, 0.5, 1.2, 7.7, 2.1, 0.6, 0.9, 0.8, 0.6), Gene4 = c(3.8, 
    0.4, 0.5, 1.2, 7.7, 2.1, 0.8, 1.8, 2.4, 3, 0.6, 0.9, 0.8, 
    0.6, 1.2, 3.8, 2.3), Gene5 = c(0.5, 1.2, 7.7, 0.9, 0.8, 0.6, 
    0.5, 1.2, 7.7, 2.1, 0.6, 0.9, 1.2, 7.7, 2.1, 0.9, 0.8)), class = "data.frame", row.names = c(NA, 
-17L))

使用上述数据,我首先在R的{​​{1}}中应用以下代码

Gene1

我想在其他基因(Gene2,Gene3,Gene4,Gene5)上应用相同的代码,并使用library(survminer) surv_rnaseq.cut <- surv_cutpoint( df, time = "years", event = "vital_status", variables = c("Gene1") ) pdf("Gene1_Cuttpt.pdf") plot(surv_rnaseq.cut, "Gene1", palette = "npg") dev.off() surv_rnaseq.cat <- surv_categorize(surv_rnaseq.cut) library(survival) fit <- survfit(Surv(years, vital_status) ~ Gene1, data = surv_rnaseq.cat) pdf("Gene1_Survival_high_vs_low_WithPvalue.pdf") ggsurvplot(fit, pval = TRUE, conf.int = FALSE, risk.table = TRUE, # Add risk table risk.table.col = "strata", # Change risk table color by groups linetype = "strata", # Change line type by groups surv.median.line = "hv", # Specify median survival ggtheme = theme_bw(), # Change ggplot2 theme palette = c("#FF0027", "#060606"), xlim = c(0,10), break.x.by = 3, xlab="Time in years", risk.table.y.text.col = T, # colour risk table text annotations. risk.table.y.text = FALSE) dev.off() 循环一次保存所有图。

谁能告诉我该怎么做。预先谢谢你。

1 个答案:

答案 0 :(得分:1)

我没有运行它,因为我没有数据。如果有错误,请提供一些数据。该代码将是这样的

library(survminer)
library(survival)
# vector with the variables to run through
genes <- c("Gene1", "Gene2", "Gene3", "Gene4", "Gene5") 

for(i in 1:length(genes)){
surv_rnaseq.cut <- surv_cutpoint(
  df,
  time = "years",
  event = "vital_status",
  variables = c(genes[i]))

pdf(paste0(genes[i], "_Cuttpt.pdf"))
print(
plot(surv_rnaseq.cut, genes[i], palette = "npg")
)
dev.off()

surv_rnaseq.cat <- surv_categorize(surv_rnaseq.cut)


fit <- survfit(as.formula(paste0("Surv(years, vital_status) ~", genes[i])),
               data = surv_rnaseq.cat)

pdf(paste0(genes[i], "_Survival_high_vs_low_WithPvalue.pdf"))

print(
ggsurvplot(fit,
        pval = TRUE, conf.int = FALSE,
       risk.table = TRUE, # Add risk table
       risk.table.col = "strata", # Change risk table color by groups
       linetype = "strata", # Change line type by groups
       surv.median.line = "hv", # Specify median survival
       ggtheme = theme_bw(), # Change ggplot2 theme
       palette = c("#FF0027", "#060606"),
       xlim = c(0,10),
       break.x.by = 3,
       xlab="Time in years",
       risk.table.y.text.col = T, # colour risk table text annotations.
       risk.table.y.text = FALSE)
)
dev.off()
}

因此,基本上,您可以使用paste()函数,并从载体基因中选择带有i的基因1-5。这将替换代码中的“ Gene1”的情况。对于survfit中的公式,您不仅可以简单地使用字符,而且还可以通过将它们包装到as.formula()中来使用。其他所有内容几乎都保持不变,并且pdf()依此类推,将创建使用基因[i]选择的基因图。