Search code examples
rplottext

add a data.frame as textbox to a plot


Is there an easier way to add a data.frame as a "textbox" to a plot? I did it "manually", using function legend.

plot(density(rnorm(1000)), xlab='x', ylab='y', lwd=1, main='Call', col='blue', type='l')
lines(density(rnorm(1000, 0, 2)), lty=3, col='red')
eo1 <- c(rnorm(1), rnorm(1), rnorm(1))
eo1 <- cbind(eo1, c(rnorm(1), rnorm(1), rnorm(1)))
colnames(eo1) <- c('contextA', 'contextB')
legend('topleft', legend=c(paste('medida', paste(colnames(eo1)[1:2], collapse='  '))
                           , paste('mean       ', paste(sprintf('%+.3f', eo1[2, 1:2]), collapse='     '))
                           , paste('sk            ', paste(sprintf('%+.3f', eo1[2, 1:2]), collapse='     '))
                           , paste('krtE         ', paste(sprintf('%+.3f', eo1[3, 1:2]), collapse='     '))
), horiz=F, cex=.7)

I could not see how Extent of boundary of text in R plot could help.
plot(density(rnorm(1000)),xlab='x',ylab='y',lwd=1,main='Call',col='blue',type='l');
lines(density(rnorm(1000,0,2)),lty=3,col='red')
eo1=cbind(rnorm(1),rnorm(1)); eo1=rbind(eo1,cbind(rnorm(1),rnorm(1)))
eo1=rbind(eo1,cbind(rnorm(1),rnorm(1)))
colnames(eo1)=c('contextA','contextB')
legend('topleft',legend=c(paste('medida',paste(colnames(eo1)[1:2],collapse='  '))
,paste('mean       ',paste(sprintf('%+.3f',eo1[2,1:2]),collapse='     '))
,paste('sk            ',paste(sprintf('%+.3f',eo1[2,1:2]),collapse='     '))
,paste('krtE         ',paste(sprintf('%+.3f',eo1[3,1:2]),collapse='     '))
),horiz=F,cex=.7)


Solution

  • What about {plotrix}:

    library(plotrix)
    
    plot(1:10)
    addtable2plot(head(iris), x = 1, y = 10, yjust = 0)
    

    plotrix example