2016-10-12 49 views
0

我創建了一個簡單的情節,我保存爲PNG(順便說一句,這是身體質量指數法語):用R將png導入pdf:爲什麼它看起來很糟糕?

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庫的各種參數,但至今未找到線索。

+0

BMI爲kg /平方公尺。嘗試以更高的分辨率渲染,然後縮小PNG的大小。 –

回答

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

謝謝,但我想結合不同的情節到類似報告 –

+2

@XavierPrudent你看了Rmarkdown製作PDF報告嗎? –

+0

感謝理查德,我將切換到Rmarkdown,另一個來自RStudio的真棒工具 –

相關問題