【问题标题】:Importing png into pdf with R: why does it look so bad?使用 R 将 png 导入 pdf:为什么看起来这么糟糕?
【发布时间】:2017-02-20 17:44:13
【问题描述】:

我创建了一个简单的图,并保存为 png(顺便说一句,即法语中的身体质量指数):

png("test.png", res=72)
imc <- 20
par(mai=c(2.8,0.2,0.2,0.2))
y.up = 0.99
plot(1, type = "n", axes = F, xlab = "kilogrammes", ylab = "", xlim = c(0, 40) )
imcVal = c(0, 16.5, 18.5, 25, 30, 35, 40, 45)
text( x = 10, y = y.up * 1.2, adj=c(0,0), "Indice de Masse Corporelle", cex=2)
rect(xleft = 0, xright = 16.5, ybottom = 0, ytop = y.up, border = NA, col = "orange")
rect(xleft = 16.5, xright = 18.5, ybottom = 0, ytop = y.up, border = NA, col = "yellow")
rect(xleft = 18.5, xright = 25, ybottom = 0, ytop = y.up, border = NA, col = "cyan")
rect(xleft = 25, xright = 30, ybottom = 0, ytop = y.up, border = NA, col = "yellow")
rect(xleft = 30, xright = 35, ybottom = 0, ytop = y.up, border = NA, col = "orange")
rect(xleft = 35, xright = 40, ybottom = 0, ytop = y.up, border = NA, col = "red")       
points(x = imc, y = y.up * 1.05, pch = 25, col = "black", bg = "black", cex = 4)
text(x = 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Famine")
text(x = 16.5 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Malnutrition")
text(x = 18.5 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Normal")
text(x = 25 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Surpoids")
text(x = 30 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Obésité modérée")
text(x = 35 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Obésité sévère")
dev.off()

现在我使用 rasterImage 库将其导入 pdf,我将包含其他绘图,因此我创建了一个矩阵布局:

library(png)
f <- readPNG("test.png")
pdf("test.pdf",paper="a4")
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
plot( 0, xaxt='n', yaxt='n', xlim=c(0,1), ylim=c(0,1), xlab="", ylab="", axes=F )
lim <- par()
rasterImage( f, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4] )
dev.off()

我得到了这个可怕的低分辨率图......

我深入研究了png和pdf库的各种参数,但到目前为止还没有找到线索。

【问题讨论】:

  • BMI 为 kg/m^2。尝试以更高的分辨率渲染,然后缩小 PNG。

标签: r pdf plot png


【解决方案1】:

可以直接使用pdf函数

pdf("test.pdf")
imc <- 20
par(mai=c(2.8,0.2,0.2,0.2))
y.up = 0.99
plot(1, type = "n", axes = F, xlab = "kilogrammes", ylab = "", xlim = c(0, 40) )
imcVal = c(0, 16.5, 18.5, 25, 30, 35, 40, 45)
text( x = 10, y = y.up * 1.2, adj=c(0,0), "Indice de Masse Corporelle", cex=2)
rect(xleft = 0, xright = 16.5, ybottom = 0, ytop = y.up, border = NA, col = "orange")
rect(xleft = 16.5, xright = 18.5, ybottom = 0, ytop = y.up, border = NA, col = "yellow")
rect(xleft = 18.5, xright = 25, ybottom = 0, ytop = y.up, border = NA, col = "cyan")
rect(xleft = 25, xright = 30, ybottom = 0, ytop = y.up, border = NA, col = "yellow")
rect(xleft = 30, xright = 35, ybottom = 0, ytop = y.up, border = NA, col = "orange")
rect(xleft = 35, xright = 40, ybottom = 0, ytop = y.up, border = NA, col = "red")       
points(x = imc, y = y.up * 1.05, pch = 25, col = "black", bg = "black", cex = 4)
text(x = 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Famine")
text(x = 16.5 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Malnutrition")
text(x = 18.5 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Normal")
text(x = 25 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Surpoids")
text(x = 30 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Obésité modérée")
text(x = 35 + 0.1, y = y.up * 0.95, srt = -90, adj = c(0, 0), "Obésité sévère")
dev.off()

【讨论】:

  • 谢谢,但我想在之后将不同的情节组合成一个类似报告的情节
  • @XavierPrudent 你看过 Rmarkdown 来制作 PDF 报告吗?
  • 谢谢 Richard,我将切换到 Rmarkdown,RStudio 的另一个很棒的工具
猜你喜欢
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 2011-04-28
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多