Adding geom_hline with mean data using for loop

42 views Asked by At

I have the following dataset and I managed to write a for loop code to plot the 13 different cytokines (analyte).

structure(list(studienr = c(1, 1, 1, 1, 1, 1), treat = structure(c(2L, 
2L, 2L, 2L, 2L, 2L), levels = c("pre", "treat", "post", "> 7days"
), class = "factor"), analyte = c("ifn_y", "il_10", "il_1b", 
"il_2", "il_4", "il_6"), groep = c("A", "A", "A", "A", "A", "A"
), result = c(3.64, 9.2622646191571, 16.4787633308804, 3.93694055601377, 
6.85511802921, 0.095), uloq = c(2679.87, 2452.78, 1344.02, 1343.09, 
1139.58, 1221.34), lloq = c(7.28, 2.21, 0.78, 2.77, 0.58, 0.19
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), groups = structure(list(studienr = c(1, 1, 1, 1, 1, 1), 
    treat = structure(c(2L, 2L, 2L, 2L, 2L, 2L), levels = c("pre", 
    "treat", "post", "> 7days"), class = "factor"), analyte = c("ifn_y", 
    "il_10", "il_1b", "il_2", "il_4", "il_6"), .rows = structure(list(
        1L, 2L, 3L, 4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .drop = TRUE))

Here is my for loop code to build the graphs

analyte <- unique(patients$analyte)

for (i in analyte){
  plot = ggplot(data = subset(cytokines, analyte == i))+
  aes(x = treat, y = result, color = groep)+
    geom_point(shape = 1, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1))+
  stat_summary(fun = mean, geom = "crossbar", width = 0.3, mapping = aes(group = groep),
        position=position_dodge(0.5))+
  scale_y_log10()+
  theme(legend.position="bottom")+ 
  ggtitle(i)+
  labs(
    y = "pg/ml", 
    x = "Time" 
  )  

ggsave(plot, file=paste0("plot_", i, ".png"))
}

This work fine, but I want to add a horizontal line for each cytokine which represents the detection level (uloq and lloq) for each cytokine in my experiment. However, each cytokine has a different detection level, so I want that my code picks the correct uloq and lloq for each cytokine.

I made a seperate dataframe with this data

structure(list(analyte = c("ifn_y", "il_10", "il_1b", "il_2", 
"il_4", "il_6", "il_8", "il12p70", "il17a", "ip_10", "mcp_1", 
"tgfb1", "tnf_a"), lloq = c(9.73313725490196, 1.90233333333333, 
4.00292134831461, 8.63331460674157, 0.786, 1.17467647058824, 
10.0087078651685, 4.15992156862745, 3.47529411764706, 2.91245098039216, 
4.37838951310861, 4.39382352941176, 8.04950980392157), uloq = c(2912.80799019608, 
2804.42256862745, 1516.49994382022, 1511.14992509363, 1360.9088627451, 
1427.64808823529, 2379.81649812734, 2641.70678431373, 3157.98093137255, 
861.867745098039, 2311.54715355805, 1781.25266666667, 1197.01573529412
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-13L))

I tried to refer to this data, but that did not work.

I tried several things like

for (i in analyte){
  plot = ggplot(data = subset(cytokines, analyte == i))+
  aes(x = treat, y = result, color = groep)+
    geom_point(shape = 1, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1))+
  stat_summary(fun = mean, geom = "crossbar", width = 0.3, mapping = aes(group = groep),
        position=position_dodge(0.5))+
  scale_y_log10()+
    geom_hline(data = subset(loq, analyte == i))+
    geom_hline(aes(yintercept = lloq), loq)+
  theme(legend.position="bottom")+ 
  ggtitle(i)+
  labs(
    y = "pg/ml", 
    x = "Time" 
  )  

ggsave(plot, file=paste0("plot_", i, ".png"))
}

But this gave the following error

Error in check_required_aesthetics(): ! geom_hline requires the following missing aesthetics: yintercept

I tried several things, but so far I only got that all detection levels were plotted (which I do not need) Example plot with all detection limits

How can I get this to work....

1

There are 1 answers

1
stefan On

Combine your both approaches to add the lines and you should be fine, i.e. do geom_hline(data = subset(loq, analyte == i), aes(yintercept = lloq)):

library(ggplot2)

analyte <- unique(cytokines$analyte)[1:2]

for (i in analyte) {
  plot <- ggplot(data = subset(cytokines, analyte == i)) +
    aes(x = treat, y = result, color = groep) +
    geom_point(shape = 1, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1)) +
    stat_summary(
      fun = mean, geom = "crossbar", width = 0.3, mapping = aes(group = groep),
      position = position_dodge(0.5)
    ) +
    scale_y_log10() +
    geom_hline(data = subset(loq, analyte == i), aes(yintercept = lloq)) +
    theme(legend.position = "bottom") +
    ggtitle(i) +
    labs(
      y = "pg/ml",
      x = "Time"
    )

  print(plot)
}