Импорт png в pdf с помощью R: почему это выглядит так плохо?

#r #pdf #plot #png

#r #PDF #сюжет #png

Вопрос:

Я создаю простой график, который сохраняю в формате 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()
  

plotpng

Теперь я импортирую его в PDF, используя библиотеку rasterImage, у меня будут другие графики для включения, поэтому я создаю макет матрицы:

 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. ИМТ равен кг / м ^ 2. Попробуйте выполнить рендеринг с более высоким разрешением, а затем уменьшите масштаб 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()
  

Комментарии:

1. Спасибо, но я хочу впоследствии объединить разные графики в один, похожий на отчет

2. @XavierPrudent Вы смотрели Rmarkdown для создания отчетов в формате PDF?

3. Спасибо, Ричард, я переключусь на Rmarkdown, еще один потрясающий инструмент от RStudio