Home > OS >  ggtitle not printing variable name in R function
ggtitle not printing variable name in R function

Time:08-28

I have created an R function to create stacked boxplots so I can change the variables to create different plots. However, when it comes to printing the title (either ggtitle or labs(title)), the function cannot identify the SNP variable.

Error in dots_list(..., title = title, subtitle = subtitle, caption = caption, : object 'chrD3.54068099' not found

This is the script I have used.

boxplot_function <- function(Group,SNP) {
  Group %>%
    group_by(`Pheno`,{{SNP}}) %>%
    count({{SNP}}) %>%
    group_by(`Pheno`) %>%
    mutate(`Proportion` = prop.table(`n`)) %>%
    ggplot(aes(x = `Pheno`, y = `Proportion`, fill = factor({{SNP}}, levels = c("HOM_ALT", "HET", "HOM_REF"))))   
    geom_bar(position = "stack", stat = "identity")  
    labs(x = "Phenotype")  
    labs(fill = "Genotype")  
    geom_text(aes(label = round(`Proportion`, 2)), position = "stack", vjust = 2)  
    ggtitle({{SNP}})
}

If I remove the script's ggtitle/labs(title) part, the function works perfectly.

I have tried different variations, such as using paste/paste0, both of which do not work. When using "{{SNP}}" it just pastes "{{SNP}}" as the title.

If you can help it would be very much appreciated! Thank you in advance!!

dput:

> dput <- dput(head(Superelaxed_vcf_topvariants,5))
structure(list(Breed_ID = c("DSH_2220", "DSH_3212", "DSH_3118", 
"DSH_2924", "DSH_2529"), IID = c(2220, 3212, 3118, 2924, 2529
), Pheno = c("CONTROL", "CONTROL", "CONTROL", "CONTROL", "CONTROL"
), chrA1.135833172 = c("HOM_REF", "HET", "HET", "HET", "HET"), 
    chrUn.19473157 = c("HOM_REF", "HOM_REF", "HET", "HOM_ALT", 
    "HOM_REF"), chrA3.73051425 = c("HOM_REF", "HOM_REF", "HOM_REF", 
    "HOM_REF", "HOM_REF"), chrB2.93712886 = c("HOM_REF", "HET", 
    "HOM_REF", "HET", "HOM_REF"), chrUn8.11195498 = c("HOM_REF", 
    "HET", "HET", "HET", "HET"), chrB3.105804049 = c("HOM_REF", 
    "HOM_REF", "HOM_REF", "HOM_REF", "HOM_REF"), chrD3.13004058 = c("HET", 
    "HOM_REF", "HOM_REF", "HET", "HOM_REF"), chrD3.14773463 = c("HET", 
    "HOM_REF", "HOM_ALT", "HOM_ALT", "HOM_REF"), chrD3.54068099 = c("HOM_REF", 
    "HOM_REF", "HOM_REF", "HOM_REF", "HOM_ALT"), chrF2.7367258 = c("HET", 
    "HET", "HOM_REF", "HOM_REF", "HOM_REF"), chrF2.9280651 = c("HOM_REF", 
    "HOM_REF", "HOM_REF", "HOM_REF", "HOM_REF")), row.names = c(NA, 
5L), class = "data.frame")

Example SNP: chrF2.9280651

CodePudding user response:

edit

You should remove the levels in your factor like this:

SNP <- "chrUn.19473157"
library(ggplot2)
library(dplyr)
boxplot_function <- function(Group,SNP) {
  Group %>%
    group_by(`Pheno`,!!SNP) %>%
    count(!!SNP) %>%
    group_by(`Pheno`) %>%
    mutate(`Proportion` = prop.table(`n`)) %>%
    ggplot(aes(x = `Pheno`, y = `Proportion`, fill = factor(!!SNP)))   
    geom_bar(position = "stack", stat = "identity")  
    labs(x = "Phenotype")  
    labs(fill = "Genotype")  
    geom_text(aes(label = round(`Proportion`, 2)), position = "stack", vjust = 2)  
    ggtitle(SNP)
}

boxplot_function(data, SNP)

Created on 2022-08-27 with reprex v2.0.2

First of all, I removed the fill part in aes because your data doesn't represent all levels of your factor, but you can just remove the # in the code. You could use !! instead of {{}} and remove the {{}} in ggtitle and resulting in:

data <- structure(list(Breed_ID = c("DSH_2220", "DSH_3212", "DSH_3118", 
                                    "DSH_2924", "DSH_2529"), IID = c(2220, 3212, 3118, 2924, 2529
                                    ), Pheno = c("CONTROL", "CONTROL", "CONTROL", "CONTROL", "CONTROL"
                                    ), chrA1.135833172 = c("HOM_REF", "HET", "HET", "HET", "HET"), 
                       chrUn.19473157 = c("HOM_REF", "HOM_REF", "HET", "HOM_ALT", 
                                          "HOM_REF"), chrA3.73051425 = c("HOM_REF", "HOM_REF", "HOM_REF", 
                                                                         "HOM_REF", "HOM_REF"), chrB2.93712886 = c("HOM_REF", "HET", 
                                                                                                                   "HOM_REF", "HET", "HOM_REF"), chrUn8.11195498 = c("HOM_REF", 
                                                                                                                                                                     "HET", "HET", "HET", "HET"), chrB3.105804049 = c("HOM_REF", 
                                                                                                                                                                                                                      "HOM_REF", "HOM_REF", "HOM_REF", "HOM_REF"), chrD3.13004058 = c("HET", 
                                                                                                                                                                                                                                                                                      "HOM_REF", "HOM_REF", "HET", "HOM_REF"), chrD3.14773463 = c("HET", 
                                                                                                                                                                                                                                                                                                                                                  "HOM_REF", "HOM_ALT", "HOM_ALT", "HOM_REF"), chrD3.54068099 = c("HOM_REF", 
                                                                                                                                                                                                                                                                                                                                                                                                                  "HOM_REF", "HOM_REF", "HOM_REF", "HOM_ALT"), chrF2.7367258 = c("HET", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 "HET", "HOM_REF", "HOM_REF", "HOM_REF"), chrF2.9280651 = c("HOM_REF", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            "HOM_REF", "HOM_REF", "HOM_REF", "HOM_REF")), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        5L), class = "data.frame")

SNP <- "chrF2.9280651"
library(ggplot2)
library(dplyr)
boxplot_function <- function(Group,SNP) {
  Group %>%
    group_by(`Pheno`,!!SNP) %>%
    count(!!SNP) %>%
    group_by(`Pheno`) %>%
    mutate(`Proportion` = prop.table(`n`)) %>%
    ggplot(aes(x = `Pheno`, y = `Proportion`))   #, fill = factor(!!SNP, levels = c("HOM_ALT", "HET", "HOM_REF"))))   
    geom_bar(position = "stack", stat = "identity")  
    labs(x = "Phenotype")  
    labs(fill = "Genotype")  
    geom_text(aes(label = round(`Proportion`, 2)), position = "stack", vjust = 2)  
    ggtitle(SNP)
}

boxplot_function(data, SNP)

Created on 2022-08-27 with reprex v2.0.2

CodePudding user response:

I figured out a way - I had to change the object into a character string within my function.

boxplot_function <- function(Group,SNP) {
  SNP_name <- deparse(substitute(SNP))
  Group %>%
    select(`Pheno`,{{SNP}}) %>%
    group_by(`Pheno`) %>%
    count({{SNP}}) %>%
    group_by(`Pheno`) %>%
    mutate(`Proportion` = prop.table(`n`)) %>%
    ggplot(aes(x = `Pheno`, y = `Proportion`, fill = factor({{SNP}}, levels = c("HOM_ALT", "HET", "HOM_REF"))))   
    geom_bar(position = "stack", stat = "identity")  
    labs(x = "Phenotype")  
    labs(fill = "Genotype")  
    geom_text(aes(label = round(`Proportion`, 2)), position = "stack", vjust = 2)  
    ggtitle(SNP_name)
}

boxplot_function(dput,chrUn.19473157)
  • Related