使用 mapply 创建多个地块

问题描述 投票:0回答:0

如果我使用第一段代码为特定基因和 SNP 绘制图表,我可以获得如下所示的图表 plot image

rs_total = read.csv("~/rs_130.csv", header = T, row.names = 1, check.names = FALSE )
rs_info = data.frame(t(rs_total))

xp_total = read.csv("~/xp_130.csv", header = T, row.names = 1,check.names = FALSE )
xp_info = data.frame(t(xp_total))

samp_id = colnames(rs_total, do.NULL = TRUE, prefix = "col")

rs_df = read.csv("~/Gene1_SNPs.csv", header = F,check.names = FALSE)

###############################################################################
  rs80002225 = data.frame(rs_info$rs80002225)
  Gene1 = data.frame(xp_info$Gene1)
  rs80002225 = cbind(rs80002225, samp_id)
  Gene1 = cbind(Gene1, samp_id)
  rs80002225_Gene1 = rs80002225 %>% inner_join( Gene1, by=c('samp_id'='samp_id'))
  colnames(rs80002225_Gene1)[1] = "rs80002225"
  colnames(rs80002225_Gene1)[3] = "Gene1"
  rs80002225_Gene1 %>%
    mutate(rs80002225 = factor(rs80002225,
                               levels=c("0",
                                        "1",
                                        "2")))
  zero_color = "#bf75d1"
  one_color =  "#778fe6"
  two_color = "#86b08e"
  
  rs_count = rs80002225_Gene1 %>%
    count(rs80002225)
  zero_n = rs_count %>%
    filter(rs80002225 == "0") %>%
    pull(n)
  one_n = rs_count %>%
    filter(rs80002225 == "1") %>%
    pull(n)
  two_n = rs_count %>%
    filter(rs80002225 == "2") %>%
    pull(n)
  
  
  rs80002225_Gene1 %>%
  ggplot(aes(x=rs80002225, y=Gene1, fill=factor(rs80002225))) +
    geom_violin(trim = FALSE)+
    # geom_boxplot(show.legend=FALSE, outlier.shape=NA, alpha=0.25, width=0.6,
    #              coef=0)+
    stat_summary(fun.data = median_hilow, fun.args=0.50, show.legend=FALSE,
                 geom="crossbar", alpha=0.25, width=0.6) +
    geom_jitter(show.legend=FALSE, width=0.25, shape=21, color="black") +
    scale_fill_manual(values = c(zero_color, one_color, two_color)) +
    labs(x = "rs80002225", y = "Gene1_Expression", fill = "")+
  theme_classic() +
  theme(axis.text.x = element_markdown())

ggsave(paste0("~/Buetow lab/eqtl_analysis/MatrixEQTL/violin/", "Gene1", "_", "rs80002225", ".png"), device="png")

然而,当我尝试简化它(如下所示)以便我可以将多个基因型绘制到一个基因时,我收到此错误消息:

Error in data.frame(..., check.names = FALSE) :  arguments imply differing number of rows: 0, 130

RS_plots = function(rs_id, gene_id){
  rs_id = data.frame(rs_info$rs_id)
  gene_id = data.frame(xp_info$gene_id)
  rs_id = cbind(rs_id, samp_id)
  gene_id = cbind(gene_id, samp_id)
  rs_id_gene_id = rs_id %>% inner_join( gene_id, by=c('samp_id'='samp_id'))
  colnames(rs_id_gene_id)[1] = "rs_id"
  colnames(rs_id_gene_id)[3] = "gene_id"
  rs_id_gene_id %>%
    mutate(rs_id = factor(rs_id,
                               levels=c("0",
                                        "1",
                                        "2")))
  zero_color = "#bf75d1"
  one_color =  "#778fe6"
  two_color = "#86b08e"
  
  rs_count = rs_id_gene_id %>%
    count(rs_id)
  zero_n = rs_count %>%
    filter(rs_id == "0") %>%
    pull(n)
  one_n = rs_count %>%
    filter(rs_id == "1") %>%
    pull(n)
  two_n = rs_count %>%
    filter(rs_id == "2") %>%
    pull(n)
  
  
  rs_id_gene_id %>%
  ggplot(aes(x=rs_id, y=gene_id, fill=factor(rs_id))) +
    geom_violin(trim = FALSE)+
    # geom_boxplot(show.legend=FALSE, outlier.shape=NA, alpha=0.25, width=0.6,
    #              coef=0)+
    stat_summary(fun.data = median_hilow, fun.args=0.50, show.legend=FALSE,
                 geom="crossbar", alpha=0.25, width=0.6) +
    geom_jitter(show.legend=FALSE, width=0.25, shape=21, color="black") +
    scale_fill_manual(values = c(zero_color, one_color, two_color)) +
    labs(x = "rs_id", y = "gene_id_Expression", fill = "")+
  theme_classic() +
  theme(axis.text.x = element_markdown())

ggsave(paste0("~/violin/", gene_id, "_", rs_id, ".png"), device="png")
}
  
hopefully = mapply(FUN = RS_plots, rs_id = rs_df$V1, gene_id = "gene_id")  

我不知道如何解决它任何指导表示赞赏! https://github.com/dezelota/M-stuff

r mapply violin-plot
© www.soinside.com 2019 - 2024. All rights reserved.