Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将通用图例添加到多个过程图中_R_Plot_Legend_Par - Fatal编程技术网

R 将通用图例添加到多个过程图中

R 将通用图例添加到多个过程图中,r,plot,legend,par,R,Plot,Legend,Par,我已经用pROC绘制了多条ROC曲线,现在我想在绘图空间的底部中心水平添加一个公共图例。我通常使用的是ggplot,现在我对基本的R绘图感到迷茫 这是我的密码: # Set plotting parameters par(mfrow=c(1,2), pty = "s", oma = c(4,1,1,1)) # ROC plot 1 roc(logit_A$y, logit_A$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE,

我已经用
pROC
绘制了多条ROC曲线,现在我想在绘图空间的底部中心水平添加一个公共图例。我通常使用的是
ggplot
,现在我对基本的R绘图感到迷茫

这是我的密码:

# Set plotting parameters
par(mfrow=c(1,2), pty = "s", oma = c(4,1,1,1))

# ROC plot 1
roc(logit_A$y, logit_A$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE, col="salmon", lwd=2, print.auc=TRUE)
plot.roc(logit_B$y, logit_B$fitted.values, percent=TRUE, col="goldenrod", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=40)
plot.roc(logit_C$y, logit_C$fitted.values, percent=TRUE, col="lightsteelblue", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=30)
title(main = "Model 1", line = 2.5)

# ROC plot 2
roc(logit_A2$y, logit_A2$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE, col="salmon", lwd=2, print.auc=TRUE)
plot.roc(logit_B2$y, logit_B2$fitted.values, percent=TRUE, col="goldenrod", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=40)
plot.roc(logit_C2$y, logit_C2$fitted.values, percent=TRUE, col="lightsteelblue", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=30)
title(main = "Model 2", line = 2.5)

# Add legend
legend("bottom",
       legend=c("A", "B", "C"),
       col=c("salmon", "goldenrod", "lightsteelblue"),
       lwd=4, cex =0.4, xpd = TRUE, horiz = TRUE)

如何使图例位于两个绘图之间底部的中心?

您的意思是这样的:

  m <- matrix(c(1,2,3,3),nrow = 2,ncol = 2,byrow = TRUE)

layout(mat = m,heights = c(0.4,0.4))

for (i in 1:2){
  par(mar = c(2,2,1,1))
  plot(runif(5),runif(5),xlab = "",ylab = "")
}


par(mar=c(0,0,1,0))
plot(1, type = "n", axes=FALSE, xlab="", ylab="")
plot_colors <- c("blue","green","pink")
legend(x = "top",inset = 0,
       legend = c("AUC:72.9%", "AUC: 71.0%", "AUC:80.0%"), 
       col=plot_colors, lwd=7, cex=.7, horiz = TRUE)

m你的意思是这样的吗:

  m <- matrix(c(1,2,3,3),nrow = 2,ncol = 2,byrow = TRUE)

layout(mat = m,heights = c(0.4,0.4))

for (i in 1:2){
  par(mar = c(2,2,1,1))
  plot(runif(5),runif(5),xlab = "",ylab = "")
}


par(mar=c(0,0,1,0))
plot(1, type = "n", axes=FALSE, xlab="", ylab="")
plot_colors <- c("blue","green","pink")
legend(x = "top",inset = 0,
       legend = c("AUC:72.9%", "AUC: 71.0%", "AUC:80.0%"), 
       col=plot_colors, lwd=7, cex=.7, horiz = TRUE)

是的,这就是我的意思。但我无法将其传输到我的代码中。你能解释一下这里发生了什么事吗?我知道你从来没有使用过
par()
?@user72716实际上很简单!我们定义3x3布局。在for循环中,我们生成6个绘图,并将它们放在为矩阵m中的索引7定义的最后一行定义的布局中,我们只生成一个空绘图,定义图例,就这样!在你的例子中,你的m应该是2by2矩阵……我把例子改成了2图!是的,这就是我的意思。但我无法将其传输到我的代码中。你能解释一下这里发生了什么事吗?我知道你从来没有使用过
par()
?@user72716实际上很简单!我们定义3x3布局。在for循环中,我们生成6个绘图,并将它们放在为矩阵m中的索引7定义的最后一行定义的布局中,我们只生成一个空绘图,定义图例,就这样!在你的例子中,你的m应该是2by2矩阵……我把例子改成了2图!