【问题标题】:Assigning matrix name as plot title in R将矩阵名称分配为 R 中的绘图标题
【发布时间】: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))

【问题讨论】:

标签: r plot


【解决方案1】:

这是使用substitute 编写自定义函数的简单方法:

my_mtext <- function(mat)mtext(side = 3, text = substitute(mat))
plot(a)
my_mtext(a)

我承认help(substitute) 并不是世界上最有用的东西:

替换返回(未计算的)表达式expr的解析树

这意味着,substitute 返回 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)

【讨论】:

  • 非常简单和中肯,很好。怎么样:function(mat, ..., main) { if (missing(main)) main &lt;- substitute(mat); plot(mat, ..., main = main); } 更通用一点,可以将任意参数传递给下一个适当的绘图方法(即使不是矩阵)。
  • 感谢您的回答。如果我没看错,这个答案适用于使用'main'直接在函数内绘图,对吗?我如何使其适应使用多行文本?在我的实际代码中,我使用的是 ordiplot。将我的 ordiplot 放入此 plot_matrix() 会返回错误“'x' 是一个列表,但没有组件 'x' 和 'y'”。
  • 我仍然得到一个奇怪的结果,但这可能是我自己无知的结果。我很感激你的时间,并将继续关注它。我想你已经给了我我需要的核心。
  • @IanCampbell,对不起,延迟响应......不,这只是扔给你的一个选项,随意使用或忽略。谢谢。
  • 我明白了,我应该能够弄清楚这一点。这更棘手。如何让整个过程成为自定义函数,然后像我的编辑一样使用 sourcecomm 的参数?
【解决方案2】:

一种选择是将它们放在一个列表中:

data = list(
a=matrix(1:10, ncol = 10,nrow=10),
b=matrix(11:20,ncol=10,nrow=10)
)

par(mfrow=c(1,2))
for(i in names(data)){
plot(data[[i]],xlab="x",ylab="y")
mtext(side=3, i)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2012-10-20
    • 1970-01-01
    • 2020-07-27
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多