使用R将png导入pdf:为什么它看起来如此糟糕?

时间:2016-10-12 03:03:26

标签: r pdf plot png

我创建了一个简单的情节,我保存为png(BTW,即法语的身体质量指数):

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()

plotpng

现在我使用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()

我得到了这个糟糕的低分辨率情节...... plotpdf

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

1 个答案:

答案 0 :(得分:0)

您可以直接使用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()