我正在 R Studio 中生成一系列图,只有数据源在它们之间发生变化。我不想手动编辑每个标题,而是自动将绘图标题指定为数据源的名称(矩阵)。不过,我正在努力将逻辑流程概念化。过于简化的代码示例:
a<-matrix(1:10, ncol = 10,nrow=10)
b<-matrix(10:20,ncol=10,nrow=10)
plot(a)
mtext(side=3, "a") #I'm using mtext instead of main because the plotting function I use doesn't support 'main'
plot(b)
mtext(side=3, "b")
Run Code Online (Sandbox Code Playgroud)
所以在这个例子中,我希望第一个情节的主标题是“a”,第二个是“b”;但是,现在的编写方式我需要手动调整mtext(side=3,"X". 我想让 mtext 语句读取矩阵名称并使用它。我承认,这有点超出我的能力范围。任何帮助表示赞赏!
由于缺少源数据,我的实际代码将无法重现,但根据评论中的要求,代码如下:
source<-n20DO1 #source data used to generate comm
comm<-bn20DO1 #actual data being used, this is the one with the title
Low<-count(source$DObin==1)[2,2]
Low<-if(is.na(Low)) {Low <- 0} else {count(source$DObin==1)[2,2]}
Mod<-count(source$DObin==2)[2,2]
Mod<-if(is.na(Mod)) {Mod <- 0} else {count(source$DObin==2)[2,2]}
High<-count(source$DObin==3)[2,2]
High<-if(is.na(High)) {High <- 0} else {count(source$DObin==3)[2,2]}
LMH <- matrix(c(Low, Mod, High),ncol=1,byrow=TRUE)
colnames(LMH) <- c("Count")
rownames(LMH) <- c("Low","Mod", "High")
LMH <- as.table(LMH)
LMH
tries=20
NMDS20=metaMDS(comm, k=2,try=tries)
treat=c(rep("Low",Low),rep("Moderate",Mod),rep("High",High))
ordiplot(NMDS20,type="n",choices=c(1,2),xaxt="n",yaxt="n",
xlab="nmds1",ylab="nmds2")
colors=c(rep("black",Low),rep("yellow",Mod),rep("red",High))
for(i in unique(treat)) {
ordihull(NMDS20$point[grep(i,treat),],draw="polygon",
groups=treat[treat==i],col=colors[grep(i,treat)],label=F) }
orditorp(NMDS20,col=c(rep("black",Low),rep("yellow",Mod),rep("red",High)),
air=0.01,cex=1.25,display="species")
mtext(side=3,"20 DO") #This is where I want the title adjusted
mtext(side=1,"Stress =" )
mtext(side=1,padj=1, round(NMDS20$stress, 4))
Run Code Online (Sandbox Code Playgroud)
这是使用编写自定义函数的简单方法substitute:
my_mtext <- function(mat)mtext(side = 3, text = substitute(mat))
plot(a)
my_mtext(a)
Run Code Online (Sandbox Code Playgroud)
我承认这help(substitute)不是世界上最有帮助的事情:
Substitute 返回(未计算的)表达式的解析树
expr
这意味着替换返回未计算的表达式mat而不是矩阵本身。因此,您可以将该符号用作绘图中的文本。
作为旁注,帮助中的详细信息很有帮助:
替代的典型用途是为数据集和图创建信息标签。
使用您的新代码立即编辑:
makePlot <- function(source, comm) {
Low<-count(source$DObin==1)[2,2]
Low<-if(is.na(Low)) {Low <- 0} else {count(source$DObin==1)[2,2]}
Mod<-count(source$DObin==2)[2,2]
Mod<-if(is.na(Mod)) {Mod <- 0} else {count(source$DObin==2)[2,2]}
High<-count(source$DObin==3)[2,2]
High<-if(is.na(High)) {High <- 0} else {count(source$DObin==3)[2,2]}
LMH <- matrix(c(Low, Mod, High),ncol=1,byrow=TRUE)
colnames(LMH) <- c("Count")
rownames(LMH) <- c("Low","Mod", "High")
LMH <- as.table(LMH)
LMH
tries=20
NMDS20=metaMDS(comm, k=2,try=tries)
treat=c(rep("Low",Low),rep("Moderate",Mod),rep("High",High))
ordiplot(NMDS20,type="n",choices=c(1,2),xaxt="n",yaxt="n",
xlab="nmds1",ylab="nmds2")
colors=c(rep("black",Low),rep("yellow",Mod),rep("red",High))
for(i in unique(treat)) {
ordihull(NMDS20$point[grep(i,treat),],draw="polygon",
groups=treat[treat==i],col=colors[grep(i,treat)],label=F) }
orditorp(NMDS20,col=c(rep("black",Low),rep("yellow",Mod),rep("red",High)),
air=0.01,cex=1.25,display="species")
mtext(side=3, paste("20 DO", substitute(comm)) #This is where I want the title adjusted
mtext(side=1,"Stress =" )
mtext(side=1,padj=1, round(NMDS20$stress, 4))
}
makePlot(source = n20DO1, comm = bn20DO1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |