Using mapply to create multiple plots

39 Views Asked by At

If I make a plot for a specific gene and SNP using the first block of code I am able to get a plot that looks like this 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")

However when I try to streamline it (as shown below) so I can plot multiple genotypes to one gene I get this error message: 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")  

I can't figure out how to fix it any guidance is appreciated! https://github.com/dezelota/M-stuff

0

There are 0 best solutions below