-2

Because my headers contain brackets I imported my dataset using check.names = FALSE.

Homo <- read.table("20200602- Homogenate MI-Athero Millepore R file.txt", header=TRUE,
                     sep="\t", as.is=TRUE, na.strings="#NULL!", comment.char = "", check.names = FALSE)

Here I split the file

Atherohomo <- Homo[Homo$"Model Code" == "MI", ]

Here comes the loop

 for (i in names(Homo)[8:32]){

    ggplot(Atherohomo, aes_string(`Sample`, i)) + 
        geom_boxplot(show.legend = F, fill= c("#3e5fa8","#fc9e23","#2D416D","#d47800"), width = 0.4) + 
        geom_beeswarm(aes(color = `Sample`), size=2)

} 

Data from dput(head(Atherohomo)):

Atherohomo <- structure(list(`Plate number` = 43:48, `Mouse ID` = c("16-mi10262-01m", 
"16-mi10262-02m", "16-mi10263-03m", "16-mi10263-04m", "16-mi10323-04m", 
"16-mi10456-01m"), Model = c("MI 4 hours", "MI 4 hours", "MI 4 hours", 
"MI 4 hours", "MI 4 hours", "MI 4 hours"), `Model Code` = c("MI", 
"MI", "MI", "MI", "MI", "MI"), Tissue = c("Homogenate", "Homogenate", 
"Homogenate", "Homogenate", "Homogenate", "Homogenate"), Sample = c("4h riva", 
"4h riva", "4h riva", "4h riva", "4h riva", "4h Ctrl"), `Dilution factor` = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `x25 (pg/ml)` = c(57.83, 
76.29, 27.75, 18.19, 39.55, 36.69), `x1 (pg/ml)` = c(73.53, 
73.53, 69.86, 67.98, 73.53, 71.7), `x2 (pg/ml)` = c(16.57, 
16.21, 15.14, 17.46, 16.57, 14.78), `x3 (pg/ml)` = c(80.99, 
95.15, 82.43, 84.58, 66.21, 69.22), `x4 (pg/ml)` = c(20, 21.56, 
20, 20, 20, 23.11), `x5 (pg/ml)` = c(3.39, 4.35, 2.24, 2.24, 
3.2, 2.72), `IL-4 (pg/ml)` = c(0, 0, 0, 0, 0, 0), `x6 (pg/ml)` = c(5.78, 
5.78, 5.44, 5.1, 5.78, 5.1), `x7 (pg/ml)` = c(33.78, 37.65, 
5.31, 5.49, 12.8, 30.34), `x8 (pg/ml)` = c(36.34, 39.35, 36.34, 
34.33, 36.34, 36.34), `x9 (pg/ml)` = c(253.48, 228.34, 234.74, 
181.11, 247.3, 228.34), `x10 (pg/ml)` = c(43.93, 37.7, 50.19, 
53.33, 38.74, 37.18), `x11 (pg/ml)` = c("33.61", "32.01", 
"34.89", "31.69", "x12 (pg/ml)+T4", "30.42"), `x13 (pg/ml)` = c(56.33, 
58.46, 54.19, 56.33, 52.04, 54.19), `x13 (pg/ml)` = c(34.41, 
36.52, 43.86, 44.9, 40.72, 41.77), `x15 (pg/ml)` = c(286.34, 
272.07, 257.73, 254.13, 272.07, 286.34), `x16 (pg/ml)` = c(3.89, 
3.67, 3.67, 3.45, 3.45, 3.56), `x17 (pg/ml)` = c(18.44, 12.48, 
3.72, 2.51, 10.14, 17.88), `x18 (pg/ml)` = c(470.3, 418.68, 25.47, 
17.69, 142.12, 288.25), `x19 (pg/ml)` = c(219.64, 117.09, 65.48, 
59.69, 98.91, 160.71), `x20 (pg/ml)` = c(188.93, 193.6, 181.74, 
179.28, 184.16, 174.29), `x21 (pg/ml)` = c(103.52, 106.61, 
99.71, 98.41, 106, 98.41), `x22 (pg/ml)` = c(204.41, 181.82, 
175.72, 175.72, 181.82, 175.72), `x23 (pg/ml)` = c(13.98, 
14.28, 12.94, 12.79, 13.24, 12.79), `x24 (pg/ml)` = c(15.27, 
13.86, 13.14, 12.41, 13.86, 13.14)), row.names = c(NA, 6L), class = "data.frame")
Axeman
  • 32,068
  • 8
  • 81
  • 94
Jens
  • 33
  • 7
  • Please provide a reproducible example. E.g. provide `dput(head(Atherohomo))`, or a snippet from the raw data file. – Axeman Jun 05 '20 at 14:58
  • 1
    I don't see any weird column names with brackets, nor do you show a loop... is `i` a column name? Or is it a variable with a column name as a string? Have a look at [How to loop over columns in ggplot?](https://stackoverflow.com/q/4856849/903061) - switching to `aes_string` or using `rlang` for your looping variable might solve the problem, but very hard to tell without more info. – Gregor Thomas Jun 05 '20 at 15:06
  • @GregorThomas, I provided more information. Hope this helps. – Jens Jun 05 '20 at 15:43
  • With the additional context, I'd suggest [How to loop over columns in ggplot](https://stackoverflow.com/q/4856849/903061) as a duplicate. – Gregor Thomas Jun 05 '20 at 15:47

1 Answers1

0

You have a range of issues. You have the x13 column twice. The x11 column has both numbers and text. For the issue with parentheses, you do need to paste the backticks in to get aes_string to work. This works for me:

Atherohomo2 <- Atherohomo
Atherohomo2$`x13 (pg/ml)` <- NULL
Atherohomo2$`x11 (pg/ml)` <- NULL

for (i in names(Atherohomo2)[8:30]){
  p <- ggplot(Atherohomo2, aes_string('Sample', paste0('`', i, '`'))) + 
    geom_boxplot(aes(fill = Sample), show.legend = F, width = 0.4) + 
    # I don't have that package installed:
    # geom_beeswarm(aes(color = `Sample`), size=2) +
    scale_fill_manual(values = c("#3e5fa8","#fc9e23","#2D416D","#d47800"))
  print(p)
} 
Axeman
  • 32,068
  • 8
  • 81
  • 94