Home > Mobile >  how to add the names on the y axis when graphing multiple variables through a function
how to add the names on the y axis when graphing multiple variables through a function

Time:06-20

I have this function that allows me to create multiple graphs on various variables of the dataset.

However in the output on the y-axis it always put the name of the list "varlist" instead of the name of each variable in the list, i.e. insuline, glucose, hdl and ldl.

How could I do that? thank you

# Multiple box plot per group per time 
library(ggplot2)
names(dflinear) <- c("id", "group", "sex", "time", "insuline", "glucose", "hdl", "ldl")

# Create a list wherein the function will be applied to
varlist<-c(list(dflinear$insuline, dflinear$glucose, dflinear$hdl, dflinear$ldl))
names(varlist)<-c("insuline", "glucose", "hdl", "ldl")

# Create the function boxplot
A <- function (varlist) {
        dflinear %>% group_by('group')%>%
  ggplot(mapping = aes_string(x='time', y='varlist', fill='group'))  
    geom_boxplot()
}

# Apply it to the whole list and graph the plots 
plots<-lapply(varlist, FUN = A)
plots

Reproducible dataset

dflinear<- structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04", 
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", 
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20", 
"SA21", "SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28", 
"SA29", "SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36", 
"SA37", "SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44", 
"SA45", "SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52", 
"SA53", "SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61", 
"SA62", "SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69", 
"SA72", "SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79", 
"SA80", "SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87", 
"SA88", "SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96", 
"SA97", "SA99", "SA100", "SA101", "SA102", "SA103", "SA104", 
"SA105", "SA107", "SA108", "SA109", "SA110", "SA111", "SA112", 
"SA113", "SA114", "SA115", "SA116", "SA118", "SC01", "SC02", 
"SC03", "SC04", "SC05", "SC06", "SC07", "SC08", "SC09", "SC10", 
"SC11", "SC12", "SC13", "SC14", "SC15", "SC16", "SC17", "SC18", 
"SC19", "SC20", "SC21", "SC22", "SC23", "SC24", "SC25", "SC26", 
"SC27", "SC28", "SC29", "SC30", "SC31", "SC32", "SC33", "SC34", 
"SC35", "SC36", "SC37", "SC38", "M01", "M02", "M03", "M04", "M05", 
"M06", "M07", "M08", "M09", "M10", "M11", "M12", "M13", "M14", 
"M15", "M16", "M17", "M18", "M19", "M20", "M21", "M22", "M23", 
"M24", "M25", "M26", "M27", "M28", "M29", "M30", "M31", "M32", 
"M33", "M34", "M35", "M36", "M37", "M38", "M39", "M40", "M41", 
"M42", "M43", "M44", "M45", "M46", "M47", "M48", "M49", "M50", 
"M51", "M52", "M53", "SA01", "SA02", "SA03", "SA04", "SA05", 
"SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", "SA13", 
"SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20", "SA21", 
"SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28", "SA29", 
"SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36", "SA37", 
"SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44", "SA45", 
"SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52", "SA53", 
"SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61", "SA62", 
"SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69", "SA72", 
"SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79", "SA80", 
"SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87", "SA88", 
"SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96", "SA97", 
"SA99", "SA100", "SA101", "SA102", "SA103", "SA104", "SA105", 
"SA107", "SA108", "SA109", "SA110", "SA111", "SA112", "SA113", 
"SA114", "SA115", "SA116", "SA118", "SC01", "SC02", "SC03", "SC04", 
"SC05", "SC06", "SC07", "SC08", "SC09", "SC10", "SC11", "SC12", 
"SC13", "SC14", "SC15", "SC16", "SC17", "SC18", "SC19", "SC20", 
"SC21", "SC22", "SC23", "SC24", "SC25", "SC26", "SC27", "SC28", 
"SC29", "SC30", "SC31", "SC32", "SC33", "SC34", "SC35", "SC36", 
"SC37", "SC38", "M01", "M02", "M03", "M04", "M05", "M06", "M07", 
"M08", "M09", "M10", "M11", "M12", "M13", "M14", "M15", "M16", 
"M17", "M18", "M19", "M20", "M21", "M22", "M23", "M24", "M25", 
"M26", "M27", "M28", "M29", "M30", "M31", "M32", "M33", "M34", 
"M35", "M36", "M37", "M38", "M39", "M40", "M41", "M42", "M43", 
"M44", "M45", "M46", "M47", "M48", "M49", "M50", "M51", "M52", 
"M53"), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L), 
    group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L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 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), insuline = structure(c(9, 4.1, 3.3, 9.4, 22.9, 16.2, 
    8.7, 16.7, 21.2, 21, 12.8, 7.3, 38.4, 20.2, 19.6, 6.4, 18.9, 
    12.1, 8.2, 17, 15.6, 12.5, 19.1, 13.7, 8, 20.1, 19.8, 6.8, 
    15.4, 14.7, 11.9, 8.8, 7.9, 51.2, 10.8, 8.1, 28.6, 8.6, 27.9, 
    13.3, 9, 16.3, 13.3, 5.8, 27.3, 4.2, 8.2, 9.9, 20.1, 11.7, 
    8.7, 18.1, 10.9, 27.4, 14.6, 29.1, 10.2, 20.2, 9.7, 12.3, 
    18.2, 1.9, 11.6, 14.6, 7.9, 11.2, 13.8, 21.2, 23.8, 18, 23.5, 
    21.4, 11.4, 12, 6.6, 13.5, 10.4, 25.3, 56.8, 10.7, 21.5, 
    8.5, 30.2, 5.3, 7.5, 15.9, 11.6, 22.4, 25.2, 6.1, 15.1, 9.3, 
    24.3, 30.8, 8.9, 9.8, 34.1, 13.4, 23.1, 21.1, 4.8, 20.1, 
    38.5, 16.1, 34.1, 16.1, 17.7, 41.4, 20.4, 21.5, 36.3, 15.9, 
    8.8, 6.1, 29, 4, 23.1, 36.8, 16.4, 15.5, 28.8, 15.9, NA, 
    7.1, 6.1, 10, 9.1, 25.2, 19.1, 6.9, 14.7, 23.1, 19.3, 12.3, 
    7.3, 5.9, 8, 0.5, 9, 4, 10.4, 21.4, 14.6, 8.8, 24.5, 5.3, 
    9.8, 17.6, 10.2, 10.7, 23, 14.5, 4.6, 33.3, 23.3, 7.2, 3.7, 
    13.1, 6.7, 20, 7.5, 9.2, 4.5, 2.1, 7.7, 11.7, 7.6, 22.5, 
    8.8, 5.1, 14.8, 15.1, 18.8, 24.3, 14, 17.2, 16.2, 23.6, 17.4, 
    16.5, 12.1, 15.3, 11.4, 8.7, 22.6, 10.5, 7.4, 15.1, 13.1, 
    24.6, 19.3, 19.7, 14.1, 5.9, 19.7, 14.9, 5.9, 17.2, 16.9, 
    6.2, 11.2, 4.1, 10, 3.7, 3.6, 11.6, 16.9, NA, 8, 17.3, NA, 
    18.3, 4, 3.1, 26.4, 12.9, 17.9, 10.3, 22.5, NA, NA, 23.4, 
    15.1, NA, 11.9, 27, 6.2, NA, 21.5, 11.6, 15.8, 8.6, 15.2, 
    10.1, 20.6, 21.7, 45.3, 8.3, 19.5, 29.2, 21.5, 11.4, 9.5, 
    31.8, 35.3, 11.2, 15.4, NA, 8.5, 22.6, 14.3, NA, 11.8, 11.4, 
    4.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 35.8, NA, NA, 
    NA, NA, NA, 19.7, 42.8, 30.6, 12.2, 5.2, 4.9, 20.4, NA, 23.5, 
    NA, 13.6, 19.4, 6.9, 16.7, 7.2, 14.7, 59.2, 22, 41.4, 18.1, 
    10.5, 19.8, 17.4, NA, 25.9, NA, 8.3, 25.9, 5.7, 17.1, 25.2, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10.3, 9.1, 14.8, 
    13.7, 6.2, 17.9, 13.9, 14.6, 70.4, 23.6, 13.8, 15.2, 9.9, 
    14, 27.6, 14.3, 23.7, 11, 12.1, 13.5, 21, NA, 7.2, 12.3, 
    4.4, 6.2, 3.9, 15, 9.6, NA, 9, 10.3, NA, 13.3, 6, 11.3, 17.6, 
    8.5, 10, NA, 11.8, 10.4, 26.2, NA, 10, 5.7, 16.3, 4.7, 20.3, 
    7.7, 14.6, 9.4, 6.3, 10, 11.1, 6.7, 42.5, NA, NA, NA, 7.7, 
    18.6, NA, 16.7, 25.4, 21.8, 26.8, 10.2, 13.8, 11.6, 19.1, 
    8.3, 3.8, 31.1, NA, 7.1, 11.1, 8.7, 19, 16, 31.8, 11.7, 3.4, 
    17.6, 12.3, 5.1, 17.5, 6.7, 3.8, 16.6, 6.1), format.spss = "F4.2", display_width = 11L), 
    glucose = structure(c(90, 95, 79, 85, 95, 97, 86, 74, 88, 
    95, 94, 88, 86, 94, 86, 95, 97, 88, 88, 88, 83, 103, 79, 
    67, 88, 79, 90, 79, 97, 94, 85, 83, 88, 97, 81, 95, 92, 94, 
    99, 79, 83, 92, 81, 92, 79, 94, 83, 79, 81, 92, 86, 95, 92, 
    95, 92, 85, 94, 81, 86, 85, 99, 92, 85, 72, 86, 81, 79, 86, 
    97, 88, 92, 97, 83, 103, 97, 95, 85, 77, 77, 83, 99, 90, 
    77, 77, 83, 92, 88, 83, 88, 86, 88, 97, 101, 99, 88, 101, 
    94, 86, 85, 83, 86, 88, 92, 94, 94, 90, 160, 94, 83, 95, 
    97, 88, 88, 95, 90, 92, 113, 104, 85, 101, 91.8, 99, 94, 
    85, 85, 83, 86, 88, 95, 79, 101, 92, 83, 90, 85, 95, 88, 
    79, 90, 79, 94, 99, 83, 85, 85, 77, 99, 81, 92, 86.4, 95.4, 
    82.8, 73.8, 81, 90, 82.8, 79.2, 90, 82.8, 91.8, 90, 84.6, 
    84.6, 84.6, 77.4, 77.4, 75.6, 88.2, 79.2, 92, 90, 113, 81, 
    81, 81, 84.6, 88.2, 73.8, 81, 81, 82.8, 79.2, 70.2, 91.8, 
    97.2, 82.8, 70.2, 91.8, 93.6, 86.4, 93.6, 73.8, 95.4, 81, 
    97.2, 77.4, 90, 82.8, 86.4, 88.2, 88.2, 73.8, 90, 92, 83, 
    86, 99, NA, 86, 81, NA, 99, 83, 86, 76, 90, 85, 90, 92, NA, 
    NA, 79, 79, NA, 86, 81, 88, NA, 90, 86, 92, 85, 92, 83, 92, 
    90, 92, 95, 94, 88, 90, 86, 88, 101, 95, 92, 81, NA, 92, 
    90, 81, NA, 90, 81, 88, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, 85, NA, NA, NA, NA, NA, 85, 88, 86, 88, 106, 101, 88, 
    NA, 79, NA, 85, 99, 92, 79, 88, 88, 95, 81, 86, 77, 81, 92, 
    97, NA, 86, NA, 88, 94, 81, 86, 85, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, 85, 88, 95, 83, 92, 112, 94, 95, 108, 
    97, 90, 88, 86, 97, 95, 88, 90, 88, 77, 94, 81, NA, 79, 83, 
    95, 88, 81, 92, 92, NA, 88, 86, NA, 85, 85, 97, 81, 88, 90, 
    NA, 77.4, 94, 83, NA, 95, 85, 92, 83, 95, 88, 94, 94, 88, 
    77, 90, 86, 92, NA, NA, NA, 95, 92, NA, 90, 103, 90, 85, 
    92, 83, 81, 94, 81, 79, 94, NA, 92, 99, 95, 84, 95, 72, 90, 
    79, 97.5, 85, 88, 79, 81, 72, 85, 88), format.spss = "F4.2", display_width = 11L), 
    hdl = structure(c(54, 55, 48, 38, 46, 50, 45, 38, 50, 43, 
    39, 32, 35, 34, 40, 48, 53, 33, 42, 34, 41, 48, 51, 38, 53, 
    38, 37, 44, 37, 33, 54, 47, 51, 39, 44, 54, 32, 53, 39, 36, 
    58, 41, 34, 43, 40, 49, 49, 50, 37, 36, 54, 47, 35, 40, 50, 
    44, 40, 43, 45, 41, 34, 50, 46, 46, 50, 53, 53, 45, 37, 70, 
    51, 55, 51, 58, 58, 49, 44, 37, 32, 64, 41, 63, 46, 55, 46, 
    65, 43, 55, 42, 56, 39, 50, 38, 46, 45, 53, 53, 39, 45, 47, 
    48, 32, 45, 45, 36, 60, 30, 43, 43, 57, 36, 56, 45, 40, 40, 
    61, 50, 29, 55, 38, 35, 47, 42, 50, 46, 26, 60, 33, 36, 34, 
    44, 59, 45, 44, 55, 45, 53, 38, 50, 40, 57, 46, 48, 45, 43, 
    49, 53, 39, 46, 39, 36, 39, 36, 42, 40, 50, 63, 46, 45, 39, 
    43, 30, 57, 46, 40, 39, 39, 53, 40, 54, 56, 40, 37, 48, 43, 
    29, 46, 45, 82, 31, 34, 37, 41, 63, 34, 50, 37, 51, 36, 42, 
    41, 34, 55, 40, 42, 60, 36, 38, 52, 57, 48, 48, 46, 47, 50, 
    41, 48, NA, 40, 45, NA, 43, 58, 42, 48, 44, 46, 47, 55, NA, 
    NA, 38, 52, NA, 53, 31, 51, NA, 32, 51, 41, 38, 57, 36, 50, 
    41, 60, 65, 39, 52, 36, 36, 49, 43, 34, 44, 41, NA, 50, 52, 
    37, NA, 58, 45, 34, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    46, NA, NA, NA, NA, NA, 59, 55, 50, 46, 58, 58, 42, NA, 31, 
    NA, 48, 43, 66, 55, 51, 41, 50, 38, 46, 41, 43, 38, 48, NA, 
    46, NA, 56, 44, 46, 48, 49, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, 63, 41, 39, 46, 58, 53, 33, 53, 48, 33, 44, 46, 
    49, 48, 44, 55, 44, 39, 32, 46, 50, NA, 47, 53, 39, 51, 61, 
    48, 32, NA, 42, 46, NA, 49, 48, 52, 39, 40, 38, NA, 31, 46, 
    48, NA, 51, 58, 43, 49, 43, 65, 41, 61, 49, 35, 37, 36, 58, 
    NA, NA, NA, 38, 45, NA, 58, 31, 49, 52, 65, 32, 45, 39, 37, 
    41, 34, NA, 42, 51, 39, 48, 36, 35, 55, 38, 48, 53, 41, 39, 
    49, 63, 41, 47), label = "HDL-Cholesterol", format.spss = "F3.2", display_width = 11L), 
    ldl = structure(c(100, 104, 171, 153, 107, 152, 87, 101, 
    70, 137, 96, 95, 98, 94, 92, 102, 63, 104, 62, 75, 125, 117, 
    114, 132, 112, 146, 121, 91, 113, 120, 96, 96, 95, 87, 96, 
    134, 98, 92, 88, 101, 133, 113, 77, 128, 97, 169, 136, 96, 
    74, 59, 121, 66, 109, 103, 116, 86, 87, 124, 88, 94, 77, 
    98, 90, 133, 79, 78, 98, 129, 62, 62, 96, 72, 85, 98, 101, 
    132, 69, 196, 76, 125, 105, 108, 89, 108, 123, 51, 92, 50, 
    121, 105, 80, 103, 59, 96, 89, 65, 77, 90, 92, 65, 123, 96, 
    80, 128, 92, 124, 96, 83, 120, 145, 114, 134, 116, 65, 91, 
    103, 84, 123, 99, 96, 61, 82, 85, 116, 116, 113, 121, 69, 
    82, 100, 108, 99, 144, 152, 158, 128, 112, 89, 119, 61, 99, 
    147, 109, 121, 92, 115, 95, 62, 72, 130, 96, 76, 117, 96, 
    108, 131, 120, 67, 99, 105, 63, 63, 103, 128, 92, 120, 146, 
    106, 103, 94, 85, 122, 111, 102, 143, 74, 87, 80, 67, 140, 
    85, 87, 101, 94, 122, 124, 82, 150, 92, 84, 119, 98, 89, 
    97, 117, 122, 111, 86, 90, 110, 107, 150, 103, 94, 149, 159, 
    91, NA, 109, 126, NA, 167, 77, 90, 103, 80, 68, 75, 55, NA, 
    NA, 74, 113, NA, 102, 116, 84, NA, 66, 85, 114, 111, 101, 
    95, 92, 86, 96, 90, 92, 77, 91, 108, 86, 118, 85, 127, 99, 
    NA, 160, 80, 63, NA, 123, 86, 94, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, 106, NA, NA, NA, NA, NA, 70, 85, 70, 96, 
    102, 117, 101, NA, 146, NA, 94, 122, 122, 94, 110, 121, 39, 
    72, 48, 109, 110, 60, 95, NA, 83, NA, 79, 87, 113, 103, 55, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 157, 103, 56, 
    92, 114, 78, 97, 106, 117, 61, 72, 83, 91, 122, 106, 103, 
    89, 51, 89, 153, 90, NA, 132, 132, 110, 84, 84, 96, 72, NA, 
    104, 122, NA, 80, 113, 106, 62, 72, 121, NA, 102, 125, 130, 
    NA, 111, 119, 66, 109, 119, 91, 92, 120, 160, 93, 117, 126, 
    88, NA, NA, NA, 115, 100, NA, 200, 79, 95, 99, 89, 123, 108, 
    82, 108, 81, 103, NA, 103, 149, 116, 115, 122, 95, 106, 89, 
    128, 118, 123, 51, 90, 130, 119, 120), label = "LDL-Cholesterol", format.spss = "F4.2", display_width = 11L)), row.names = c(NA, 
-404L), class = c("tbl_df", "tbl", "data.frame"), reshapeLong = list(
    varying = list(c("age_1", "age_2"), c("whz_1", "whz_2"), 
        c("haz_1", "haz_2"), c("waz_1", "waz_2"), c("zbmi_1", 
        "zbmi_2"), c("wc_1", "wc_2"), c("abc_1", "abc_2"), c("PA_1", 
        "PA_2"), c("PAextra_1", "PAextra_2"), c("TVweekdays_1", 
        "TVweekdays_2"), c("TVweekend_1", "TVweekend_2"), c("kidmed_1", 
        "kidmed_2"), c("totalcholesterol_1", "totalcholesterol_2"
        ), c("ldl_1", "ldl_2"), c("hdl_1", "hdl_2"), c("triglycerides_1", 
        "triglycerides_2"), c("glucose_1", "glucose_2"), c("insuline_1", 
        "insuline_2"), c("hba1c_1", "hba1c_2"), c("homair_1", 
        "homair_2"), c("fatmass_1", "fatmass_2"), c("energykcal_1", 
        "energykcal_2"), c("protein_1", "protein_2"), c("proteinpc_1", 
        "proteinpc_2"), c("carbohydrates_1", "carbohydrates_2"
        ), c("carbohydratespc_1", "carbohydratespc_2"), c("sugar_1", 
        "sugar_2"), c("sugarpc_1", "sugarpc_2"), c("starch_1", 
        "starch_2"), c("fruitportions_1", "fruitportions_2"), 
        c("vegetablesportions_1", "vegetablesportions_2"), c("vegetalfiber_1", 
        "vegetalfiber_2"), c("solublefiber_1", "solublefiber_2"
        ), c("insolublefiber_1", "insolublefiber_2"), c("lipids_1", 
        "lipids_2"), c("lipidspc_1", "lipidspc_2"), c("sfa_1", 
        "sfa_2"), c("sfapc_1", "sfapc_2"), c("mufa_1", "mufa_2"
        ), c("mufapc_1", "mufapc_2"), c("pufa_1", "pufa_2"), 
        c("pufapc_1", "pufapc_2"), c("cholesterolintake_1", "cholesterolintake_2"
        )), v.names = c("age", "whz", "haz", "waz", "zbmi", "wc", 
    "abc", "PA", "PAextra", "TVweekdays", "TVweekend", "kidmed", 
    "totalcholesterol", "ldl", "hdl", "triglycerides", "glucose", 
    "insuline", "hba1c", "homair", "fatmass", "energykcal", "protein", 
    "proteinpc", "carbohydrates", "carbohydratespc", "sugar", 
    "sugarpc", "starch", "fruitportions", "vegetablesportions", 
    "vegetalfiber", "solublefiber", "insolublefiber", "lipids", 
    " lipidspc", "sfa", "sfapc", "mufa", "mufapc", "pufa", "pufapc", 
    "cholesterolintake"), idvar = c("id", "group"), timevar = "time"))

CodePudding user response:

Instead of making your varlist a list of vectors you could simply pass a vector with names of the colums you want to plot. Then use aes_string(..., y = varlist) inside your function and you will automatically get the name of the variable as the y axis title:

# Multiple box plot per group per time
library(ggplot2)
library(dplyr)

# Create a list wherein the function will be applied to
varlist <- c("insuline", "glucose", "hdl", "ldl")
names(varlist) <- varlist
# Create the function boxplot
A <- function(varlist) {
  dflinear %>%
    group_by("group") %>%
    ggplot(mapping = aes_string(x = "time", y = varlist, fill = "group"))  
    geom_boxplot()
}

# Apply it to the whole list and graph the plots
plots <- lapply(varlist, FUN = A)
plots[[1]]

enter image description here

  • Related