Home > Enterprise >  Looping through variables to make many boxplots
Looping through variables to make many boxplots

Time:02-19

I am using from the package OlinkAnalyze and I am trying to make box plots.

    install.packages("OlinkAnalyze")
        library(OlinkAnalyze)
df = npx_data1

the code for the boxplot is:



plot <- df %>%
  na.omit() %>% # removing missing values which exists for Site
  olink_boxplot(variable = "Site", 
                olinkid_list = c("OID01216", "OID01217"),
                number_of_proteins_per_plot  = 2)
plot[[1]]

It takes values from the olinkID column. What I would like, is to loop through the column, choosing the next two olinkID at a time, to make boxplots, renaming the plot each time (e.g.plot 1 with OID01216 and OID01217 and plot 2 with OID01218 OID01219

CodePudding user response:

If you want the loop, you could write like this:

for i in seq(from = 1, to = length(data$OlinkID), by = 2){
    the plot code
  }

This way you can access the two observations you want by data$OlinkID[i] or data$OlinkID[i 1].

So the boxplot code should be

plot <- data %>%
na.omit() %>%
olink_boxplot(variable = "Oxwatchtime", 
            olinkid_list = c(data$OlinkID[i],data$OlinkID[i 1]),
            number_of_proteins_per_plot  = 2)

If you want to save the plots, add a ggsave() or a png()/pdf() in the loop to save them externally or create a list with them using ggarrange() function from ggpubr package. Let me know if it works as you intended.

CodePudding user response:

OlinkAnalyze::olink_boxplot() plots several plots until all proteins specified under the olinkid_list argument are plotted. The number_of_proteins_per_plot argument determines the number of IDs plotted on one plot.

Try this:

library(OlinkAnalyze)
data("npx_data1")

ids <- unique(npx_data1$OlinkID)
olink_boxplot(npx_data1,
              variable = "Site", 
              olinkid_list = ids, 
              verbose = TRUE,
              number_of_proteins_per_plot  = 2)

The code runs for a while as each plot takes time to be generated. When it completes you can use the arrow buttons in RStudio to look at all the plots.

CodePudding user response:

I used a while loop.

install.packages("OlinkAnalyze")
library(OlinkAnalyze)
df = npx_data1
i <- 1

ids <- as.data.frame(unique(df$OlinkID))

while(i <= nrow(ids)){
  print(i)
  x <- i 1
  temp <- ids[i:x,]
  plotx <- df %>%
    na.omit() %>%  #
    olink_boxplot(variable = "Site", 
                  olinkid_list = c(paste(c(ids[i,],ids[x,]))),
                  number_of_proteins_per_plot  = 2)

  plottemp <- assign(paste0("plot_",ids[i,],"_",ids[i,]),plotx)
  i <- i 2
}
  • Related