在绘图的空白处打印data.frame

在绘图的空白处打印data.frame,r,plot,R,Plot,我希望将打印(df.to.print)的输出打印在绘图上。如果我能把位置放在左上角或右上角上,就像在调用“legend(…)时一样,那就太好了,但这只是一个额外的奖励 一些代码: # 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)),

我希望将
打印(df.to.print)
的输出打印在绘图上。如果我能把位置放在
左上角
右上角
上,就像在调用“legend(…)时一样,那就太好了,但这只是一个额外的奖励

一些代码:

# 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().
#要播放的数据帧
df.to.print试试这个:

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

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

据我所知,有两个函数可以执行类似的操作:plotrix软件包中的
addtable2plot
,可用于基本R图形;以及gridExtra软件包中的
grid.table
,可用于网格图形(即ggplot/lattice)

至于我是如何找到它们的:我花了相当多的时间来回答这些问题,以便在R方面做得更好,在这个过程中,我发现“在R方面做得更好”通常等同于“在文档中更好地找到答案”


sos软件包是一个良好的开端。

addtable2plot
来自plotrix?或者
grid.table
frmo gridExtra用于网格图形(ggplot/lattice)。@joran解决了我的问题。你能把它作为一个答案,并简要解释一下你是如何发现这些功能的吗?我曾尝试过谷歌搜索,但没有任何有用的结果。(也许答案是“体验”?)基本图形的每个用户都应该翻阅package
plotrix
的索引页。其中大部分内容由Jim Lemon撰写,他还负责监控rhelp邮件列表,并及时回复更新和解释请求。请参见类似问题,如@joran建议的e.g.+1,这是一个不错的基本用法小解决方案。我必须在我目前使用的WinXP机器上使用
family=“mono”
,否则我会得到一个在Windows字体数据库中找不到的
Font系列警告。+1这是一个非常酷的解决方案!感谢您指出capture.output(…)capture.output的巨大用途,我不知道!