2

我希望将 的输出print(df.to.print)打印在绘图上。如果我可以在调用 `legend(...) 时将位置放置在toplefttopright类似位置,那就太好了,但这只是一个奖励。

一些代码:

# A data.frame to play with
df.to.print <- structure(list(p.val = c(0.05, 0.1), self = c(0.0498, 0.0997), 
    H2007.REML = c(0, 0.01), H2007.ref = c(0, 0)), .Names = c("p.val", 
"self", "H2007.REML", "H2007.ref"), row.names = c(NA, -2L), class = "data.frame")

# A simple plot
plot(1)
text(1,1, df.to.print )
# All of the entries are overlapping

# I can insert newlines easily enough
plot(1)
text(1,1, paste(as.character(df.to.print), collapse='\n'))

# But that looses all of the nice formatting in the data.frame.
# Which is easy enough to get on the console with: 
print(df.to.print)

# Bonus points for 'topleft', 'topright', etc. like in legend().

任何帮助,将不胜感激。

4

2 回答 2

11

尝试这个:

plot(1)
text(0.6,1, paste(capture.output(df.to.print), collapse='\n'), pos=4, family="monospace")

pos=4将文本放置在指定坐标的右侧,并保持左对齐。也尝试使用固定间距字体。

于 2013-01-14T21:23:28.053 回答
1

我所知道的两个执行类似操作addtable2plot的函数来自plotrix包,可用于基本 R 图形,以及grid.table来自gridExtra包,可用于网格图形(即 ggplot/lattice)。

至于我是如何找到它们的:我花了很多时间回答关于 SO 的问题,以便在 R 上做得更好,在这个过程中,我发现“在 R 上做得更好”通常等同于“在 R 上做得更好”在文档中找到答案”。

sos包是一个好的开始。

于 2013-01-14T21:24:47.173 回答