Search code examples
rpdfmagick

Importing a scatter plot from PDF into R


I am trying to import a scatter plot from a PDF. The plot was originally created in R. When I import the PDF I am unable to see the scatter points. Not sure what I am doing wrong? Plots with lines seem to work fine.

A toy example to demonstrate the problem:

pdf(file = "./temp.pdf", height = 5, width = 5)
plot(mtcars$mpg, mtcars$cyl, type = "p")
dev.off()
file.show("temp.pdf")

# same issue with ggplot
# pdf(file = "./temp.pdf", height = 5, width = 5)
# ggplot(data = mtcars, mapping = aes(x = mpg, y = cyl)) +
#   geom_point()
# dev.off()

p0 <- magick::image_read_pdf('./temp.pdf')
p0

p1 <- pdftools::pdf_render_page('./temp.pdf')
png::writePNG(p1, "temp.png")
file.show("temp.png")

Both p0 and temp.png give enter image description here

> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.6.0 magrittr_1.5   magick_2.0     tools_3.6.0    Rcpp_1.0.1    
[6] pdftools_2.2   qpdf_1.1       png_0.1-7      askpass_1.1

Solution

  • For anyone in the future who gets stuck with this, useDingbats = FALSE in the pdf() function did the trick for me, i.e.

    pdf(file = "./temp.pdf", height = 5, width = 5, useDingbats = FALSE)