Reputation: 15
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:
As you can tell, the "parent group" name or the y axis labels are not necessarily unique to each variable name
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
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:
This is not exactly what you ask for but it's hopefully useful to you nevertheless.
Upvotes: 1