在R图形中打印文本块

时间:2017-11-08 00:12:47

标签: r

有没有办法使用df = data.frame(x = runif(100), y = runif(100), z = sample(0:1,100,TRUE)) fit = glm(z~x+y, data=df) par(mfrow=c(1,2)) roc(df$z, fitted(fit), plot=T, legacy.axes=T) # This should be in the second column print(summary(fit)) 在单个图形中显示文本输出旁边的图?

我试图在单个图形中显示ROC曲线(第一个图)和系数表(第二个“图”)。

var element;
if(selector.charAt(0) == "#"){
    element = document.getElementById(selector);
}else if(selector.charAt(0) == "."){
    if(!document.getElementsByClassName){
        element = document.querySelectorAll(selector);
    }else{
        element = document.getElementsByClassName(selector);
    }
}else{
    element = document.querySelectorAll(selector);
}
return element;

另外,我知道这可以用ggplot2完成。我想知道如何使用内置图形库来实现这一目标。

2 个答案:

答案 0 :(得分:3)

只需制作一个空图,然后使用text将文字放在您想要的位置。

par(mfrow=c(1,2))
plot(iris[,3:4], pch=20, col=rainbow(3)[iris$Species])
plot(NULL,xaxt='n',yaxt='n',bty='n',ylab='',xlab='',
    xlim=c(0,1), ylim=c(0,1))
text(0,0.9, pos=4,"Oh where oh where has my little dog gone?")
text(0,0.8, pos=4,"Oh where oh where can he be?")
text(0,0.7, pos=4,"with his ears cut short")
text(0,0.6, pos=4,"and his tail cut long")
text(0,0.5, pos=4,"Oh where oh where can he be?")

Picture and text

答案 1 :(得分:1)

source G5W's的帮助下,我创建了一个完整的代码段。这只是将输出捕获为一个字符串,并迭代地将它们打印成一个空图:

df = data.frame(x = runif(100), y = runif(100), z = sample(0:1, 100, TRUE))
fit = glm(z~x+y, data=df)
par(mfrow=c(1, 2), mar=c(0, 0, 0, 0), oma=c(0, 0, 0, 0))
roc(df$z, fitted(fit), plot=T, legacy.axes=T)
summ = capture.output(summary(fit))
plot(NULL, xaxt='n', yaxt='n', bty='n', ylab='', xlab='', xlim=c(0, 100), ylim=c(0, 100), xaxs = 'i', yaxs = 'i')
for (i in seq_along(summ)) {
  text(0, 100 - i*4, pos=4, summ[i], cex = 0.5, family='mono')
}

answer