【发布时间】:2020-06-16 05:39:03
【问题描述】:
我在 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")
所以在这个例子中,我希望第一个情节的主标题是“a”,第二个是“b”;但是,现在写的方式我需要手动调整mtext(side=3,"X"。我想让 mtext 语句读取矩阵名称并使用它。我承认,这有点超出我的能力范围。感谢您提供任何帮助!
由于缺少源数据,我的实际代码无法重现,但按照 cmets 的要求,如下所示:
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))
【问题讨论】: