R 如何从具有四舍五入数字的数据框中将表格导出为PNG

R 如何从具有四舍五入数字的数据框中将表格导出为PNG,r,dataframe,export,png,digits,R,Dataframe,Export,Png,Digits,我有很多数据帧,比如带有一些列和行的df。正因为如此,我搜索了一些东西,将我的表直接写为png文件。因此,我不必在另一个程序中自己生成所有表 df <- structure(c(1.688, 2.402, 2.636, 2.656, 2.8, 2.337, 0.261, 0.3, 0.299, -0.158, -0.79, -0.115, 2.196, 3.067, 3.31, 3.437, 3.526, 3.012, 1.895, 2.643, 3.31, 3.085, 3.07,

我有很多数据帧,比如带有一些列和行的
df
。正因为如此,我搜索了一些东西,将我的表直接写为png文件。因此,我不必在另一个程序中自己生成所有表

df <- structure(c(1.688, 2.402, 2.636, 2.656, 2.8, 2.337, 0.261, 0.3, 
0.299, -0.158, -0.79, -0.115, 2.196, 3.067, 3.31, 3.437, 3.526, 
3.012, 1.895, 2.643, 3.31, 3.085, 3.07, 2.735), .Dim = c(6L, 
4L), .Dimnames = list(c("U", "V", "W", "X", "Y", "Z"), c("A", 
"B", "C", "D")))
在我和最科学的工作中,数字在整个表格中应该是相同的。所以
0.300
应该写成
0.300
,而不是
0.3
。据我所知,这两个函数都不包括
圆形
数字
函数

使用@jay.sf的代码,我可以解决四舍五入数字的问题

df <- apply(df, 1:2, formatC, format="f", digits=3)  # format digits

pdf("df.pdf", height = 12, width = 10)
grid.table(df)
dev.off()

df有一种方法可以画出这个东西

df <- apply(df, 1:2, formatC, format="f", digits=3)  # format digits

# Helper sequences    
sy <- seq(.9, 0, length.out=nrow(df))
sx <- seq(0.1, 1, length.out=ncol(df))
adj <- .03  # for adjustment

png(width=600, height=400, res=100)

par(mar=c(2, 2, 4, 2) + 0.1)
plot.new()  # initialize plot
# plot column names
sapply(seq(ncol(df)), function(x) 
  text(sx[x] - adj, 1 + adj, colnames(df)[x], font=2, xpd=TRUE))
# plot row names
sapply(seq(nrow(df)), function(x) 
  text(0 - adj, sy[x], rownames(df)[x], font=2, xpd=TRUE))
# plot values
mapply(function(x, y) text(sx[x], sy[y], labels=df[y, x]), 
       rep(seq(ncol(df)), each=nrow(df)), rep(seq(nrow(df)), ncol(df)))
# plot title
text(-.05, 1.2, "Good additional title here", xpd=TRUE, cex=1.5, font=2, adj=0)

dev.off()

df谢谢你的留言@joran。我不喜欢乳胶,但我也会在那里尝试。看起来《观星者》很不错。谢谢@jay.sf。它的格式不如其他两个函数好,但我的请求到目前为止已经满足了。当然,你必须自己做一些事情,我已经向你展示了基本方法。例如,尝试在代码中添加
sapply(c(.98,1.07,1.08),函数(x)行(c(-0.04,1.04),rep(x,2),xpd=TRUE))
,现在我也知道了如何添加行,谢谢!使用代码的第一部分,我可以解决
表格中的数字问题!
df <- apply(df, 1:2, formatC, format="f", digits=3)  # format digits

# Helper sequences    
sy <- seq(.9, 0, length.out=nrow(df))
sx <- seq(0.1, 1, length.out=ncol(df))
adj <- .03  # for adjustment

png(width=600, height=400, res=100)

par(mar=c(2, 2, 4, 2) + 0.1)
plot.new()  # initialize plot
# plot column names
sapply(seq(ncol(df)), function(x) 
  text(sx[x] - adj, 1 + adj, colnames(df)[x], font=2, xpd=TRUE))
# plot row names
sapply(seq(nrow(df)), function(x) 
  text(0 - adj, sy[x], rownames(df)[x], font=2, xpd=TRUE))
# plot values
mapply(function(x, y) text(sx[x], sy[y], labels=df[y, x]), 
       rep(seq(ncol(df)), each=nrow(df)), rep(seq(nrow(df)), ncol(df)))
# plot title
text(-.05, 1.2, "Good additional title here", xpd=TRUE, cex=1.5, font=2, adj=0)

dev.off()