[英]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).一些背景:我试图分析的数据是流式细胞术数据(对于那些不熟悉这是什么的人,为了这个 Q 的目的,它基本上是使用激光来表征不同的细胞类型)。 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."
我在这里的变量是要在 y 轴上绘制的单元格的不同特征,并且每个变量都是从“父组”中读取的。 That is, if the variable of interest is Live Cells, the parent group can be Kidney cells, or Lung cells etc.
也就是说,如果感兴趣的变量是 Live Cells,则父组可以是肾细胞或肺细胞等。
I have one large data table with 13 variables that need to be plotted against the same x axis, ie, time after treatment in weeks.我有一个包含 13 个变量的大型数据表,需要针对相同的 x 轴绘制,即治疗后的时间(以周为单位)。 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.
我需要 plot 对时间的每个变量,需要不同的 y 轴 label 和一个标题,该标题应该包括y 轴 label 以及相应的“父组”变量的名称。 The steps I have taken are:
我采取的步骤是:
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如您所知,“父组”名称或 y 轴标签不一定对每个变量名称都是唯一的
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.我在这里尝试做的是自动化 for 循环以添加相应的 y 轴 label 和每个变量的标题,但我当然陷入了一个循环,它继续运行并生成每个变量 13 个 y 轴的组合标签 * 13 plot 标题组合。 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.
我需要 13 个图,每个图都有自己的 y 轴标签和 plot 标题,而无需为每个图手动输入,因为我希望将图直接从 for 循环导出为 jpeg 文件。 I thought about using if or if else functions but I am not sure how to apply those here.
我考虑过使用 if 或 if else 函数,但我不确定如何在此处应用这些函数。 Any help with this would be great appreciated!
对此的任何帮助将不胜感激!
Here's an approach that is an interpretation of your question: use facet plotting and map the Parent
and Yaxis
into the facet label.这是一种解释您的问题的方法:使用构面绘图和 map
Parent
级和Yaxis
进入构面 label。 This will result in a series of facet plot where the label reflects the Parent
and Yaxis
.这将产生一系列方面 plot 其中 label 反映
Parent
和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
:然后我们使用来自
pivot_longer
的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
:然后,我们使用
Yaxis
从映射表中引入数据,并使用基于Parent
和left_join
的构面标签创建一个新列:
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
,使用facet_labels
作为 facetting 变量:
ggplot(data = data, aes(x = Week, y = values)) +
geom_point() +
facet_wrap(.~ facet_labels, scales = "free") +
theme_minimal()
With this output:有了这个 output:
This is not exactly what you ask for but it's hopefully useful to you nevertheless.这并不完全是您所要求的,但希望它对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.