pkt
pkt

Reputation: 15

How can I create multiple plots, each with different y axis labels and titles using a for loop in R?

Some context: The data I am trying to analyse is flow cytometry data (for those not familiar with what this is, for the purpose of this Q, it basically is characterizing different cell types using lasers). My variables here are different characteristics of my cells to be plotted on the y axis and each variable is read from a "parent group." That is, if the variable of interest is Live Cells, the parent group can be Kidney cells, or Lung cells etc.

I have one large data table with 13 variables that need to be plotted against the same x axis, i.e, time after treatment in weeks. Each of the variables I need to plot against time, requires a different y axis label and a title which should include both the y axis label as well as the name of the respective "parent group" the variable belongs to. The steps I have taken are:

  1. Import the master data table into R
  2. Import a data table with keywords, i.e, each variable name (which are the column names in the master data table) alongside its respective Y axis label and "parent group" name. As an example, here's what that table looks like,

enter image description here

As you can tell, the "parent group" name or the y axis labels are not necessarily unique to each variable name

  1. Assign each column of the keywords data table to a new vector, i.e variables, yaxis and parent.
  2. Make a FOR loop to automatically subset data from the original master table containing info for one variable at a time and then plot it against time.
    r 
   for(i in variables){
    table<-as.data.frame(test[,c("WEEK",i)])`
    for(j in yaxis){
    for (k in parent){
    plot<-ggplot(table,aes(x=WEEK,y=table[,3]))+geom_line()+geom_point(size=2)+scale_shape_manual(values=1:25)+xlab("Weeks")+ylab(j)+theme_minimal()+ggtitle(paste0(k,"-",j))
ggsave(plot,file=paste0(paste(j,k,sep="_"),"plot.jpg"),width=14,height=10,units="cm")
     }
      }
      rm(table)
      rm(plot)
    }

What I tried to do here was automate the for loop to add the respective y axis label and title for each variable but I of course, got stuck in a loop and it went on an on and generated, per variable the combination of 13 y axis labels * 13 plot title combinations. I need 13 plots, each with its own y axis labels and plot titles without manually entering it for each one as I want the plots to be exported directly from the for loop as a jpeg file. I thought about using if or if else functions but I am not sure how to apply those here. Any help with this would be great appreciated!

Upvotes: 0

Views: 1384

Answers (1)

Paul van Oppen
Paul van Oppen

Reputation: 1495

Here's an approach that is an interpretation of your question: use facet plotting and map the Parent and Yaxis into the facet label. This will result in a series of facet plot where the label reflects the Parent and Yaxis.

To do facetting we need to reshape the data frame. first, call libraries and create some useful data:

library(dplyr)
library(tidyr)
library(tibble)
library(ggplot2)

# make fake data
data <- tibble(
  Week = seq(from = 1, length.out = 91),
  cells_live = runif(min = 1, max = 10, n = 91),
  cells_live_dividing = runif(min = 1, max = 10, n = 91),
  cells_live_aging = runif(min = 1, max = 10, n = 91)
)

mapping <- tibble(
  variables = c("cells_live", "cells_live_dividing", "cells_live_aging"),
  Parent = c("COUNT", "live", "live"),
  Yaxis = c("live cells", "divinding cells", "aging")
)

Then we reshape the data into a long format using pivot_longer from tidyr:

data <- data %>%
  pivot_longer(cols = -Week,
               names_to = "vars",
               values_to = "values")

We then bring in the data from the mapping table with a left_join and create a new column with the facet labels based on Parent and Yaxis:

data <- data %>%
  left_join(mapping, by= c("vars" = "variables")) %>%
  mutate(facet_labels = paste0(Parent, ": ", Yaxis))

Finally, we call ggplot where we use the facet_labels as the facetting variable:

ggplot(data = data, aes(x = Week, y = values)) +
  geom_point() +
  facet_wrap(.~ facet_labels, scales = "free") +
  theme_minimal()

With this output:

enter image description here

This is not exactly what you ask for but it's hopefully useful to you nevertheless.

Upvotes: 1

Related Questions