GGplot2 - error 'mapping' must be created by 'aes()'

Viewed 551

I am new to R and have a little problem with ggplot. Here's part of the data:

    dput(base_df[1:5, -c(1,4:18)])
base_df <- structure(list(pre_exposure = structure(c(8L, 4L, 3L, 3L, 3L), .Label = c("All seronegative", 
"B seropositive only", "H3 seropositive only", "H1 seropositive only", 
"H3 and B seropositive", "H1 and H3 seropositive", "H1 and B seropositive", 
"All seropositive"), class = "factor"), of_v0_total_iga_elisa = c(14.43528799, 
4.918994451, 27.68948908, 28.98430055, 35.08496692), ph1n1_cali_09_ha1_adjusted = c(NA, 
123.70414442657, 11.1414117865695, 8.69436195519992, 69.0324150945501
), ph1n1_mich_15_ha1_adjusted = c(NA, 52.0431568992636, 2.74472381127807, 
1.50081248036189, 74.6473555460887), h2n2_cana_05_ha1_adjusted = c(NA, 
4.57410558684934, 3.28644561613558, 1.17304883522539, 4.58885996293252
), h5n8_duck_16_ha1_adjusted = c(NA, 11.1811469900762, 2.94335513972582, 
1.43180960770157, 4.36084207657563), h5n8_turk_14_ha1_adjusted = c(NA, 
6.40374782158908, 1.46264887311529, 0.983290935409514, 2.1519188024932
), h9n2_guin_14_ha1_adjusted = c(NA, 17.2799544392086, 5.61584937702289, 
3.98491589613329, 3.52002612063458), h3n2_bris_07_ha1_adjusted = c(NA, 
133.868823508457, 103.071602070889, 25.0997949301902, 11.828427854764
), h3n2_hong_14_ha1_cell_adjusted = c(NA, 189.266324504744, 314.451450326291, 
101.192712756355, 31.3809616098678), h3n2_hong_14_ha1_egg_adjusted = c(NA, 
42.5900053531083, 24.9553178104361, 9.97091509941578, 8.97820427530276
), h7n7_chic_03_ha1_adjusted = c(NA, 5.18398633176259, 1.17373057719128, 
0.155256463485713, 0.655551423276075), b_texa_13_ha1_adjusted = c(NA, 
6.7086881940457, 3.06975689419257, 9.17738206382213, 5.05914685354362
), ph1n1_mich_15_na_adjusted = c(NA, 16.161839740201, 8.10776967936745, 
3.77790727815234, 6.56976535065805), ph1n1_cali_09_na_adjusted = c(NA, 
12.095968107446, 7.27712957858593, 4.24367666860948, 6.62676982224728
), h3n2_hong_14_na_adjusted = c(NA, 27.6479271027338, 11.8637075263795, 
9.67765289060943, 4.5318554913433), b_texa_13_na_adjusted = c(NA, 
16.7717204851142, 255.855208434203, 4.95095611337773, 64.6288196642826
)), row.names = c(NA, 5L), class = "data.frame")

I used the following packages:

library(ggplot2)
library(tidyverse)
library(ggpubr)

This is the code I ran:

antigens <- c("ph1n1_cali_09_ha1", "ph1n1_mich_15_ha1", "h2n2_cana_05_ha1", "h5n8_duck_16_ha1",
          "h5n8_turk_14_ha1", "h9n2_guin_14_ha1", "h3n2_bris_07_ha1", "h3n2_hong_14_ha1_cell",
          "h3n2_hong_14_ha1_egg", "h7n7_chic_03_ha1", "b_texa_13_ha1", "ph1n1_mich_15_na",
          "ph1n1_cali_09_na", "h3n2_hong_14_na", "b_texa_13_na")

for(antigen in antigens) {

ggplot(base_df, aes(x=pre_exposure, y=log10(eval(as.symbol(paste0(antigen, "_adjusted")))))) + 
geom_boxplot(width=0.3, na.rm = TRUE) +
geom_jitter(position=position_jitter(0.1), na.rm = TRUE) + 
stat_summary(fun=median, geom="crossbar", color="red", width = 0.3, na.rm = TRUE) + 
stat_compare_means(paste0(antigen, "_adjusted")~pre_exposure, method="kruskal.test") +
theme_classic() + 
theme(axis.text.x = element_text(angle=45, vjust = 0.5)) +
ggtitle(paste0(antigen, " specific IgA by influenza pre-exposure") ) + 
ylab("Log10 florescence / total IgA") +
xlab("Pre-exposure status") +
scale_x_discrete(limits=c("All seronegative","H1 seropositive only", "H3 seropositive only", 
                          "B seropositive only", "H1 and H3 seropositive", "H1 and B seropositive",
                          "H3 and B seropositive", "All seropositive"))

 ggsave(paste0(antigen, ".svg")) }

If I run the ggplot loop without the line stat_compare_means(paste0(antigen, "_adjusted")~pre_exposure, method="kruskal.test") R runs my code and there doesn't seem to be any problem. But once I add the stat_compare_means to add p-values to my plots, the error

'mapping' must be created by 'aes()'

pops up. Does anyone have an idea how to solve this problem?

0 Answers
Related