0

I am creating a book with bookdown and I have run the scatter3d() function from car and rgl.

I have read that 3D images can be interactivelly implemented in HTML (here), however, what I need is to implement a simply plain image in the PDF output.

How can I do it?

Let's use this example:

scatter3d(mpg~hp+qsec|factor(cyl), data=mtcars, surface=FALSE, residuals=TRUE, 
  parallel=FALSE, bg="white", axis.scales=TRUE, grid=TRUE, ellipsoid=TRUE)

EDIT 1

By employing @user2554330 's answer, I created an R Markdown file (.Rmd):

---
title: "Untitled"
author: "Mario"
date: "1 July 2019"
output:
  pdf_document: default
  html_document: default
---

```{r echo=FALSE}
library(rgl)
library(car)
setupKnitr()
```

Here's a plot:

```{r echo=TRUE, dev="png", rgl=TRUE}
scatter3d(mpg~hp+qsec|factor(cyl), data=mtcars, surface=FALSE, residuals=TRUE,
          parallel=FALSE, bg="white", axis.scales=TRUE, grid=TRUE, ellipsoid=TRUE)    
```

However, when I convert my .Rmd file to PDF, I do not see the plot:

enter image description here

Where's the problem?

antecessor
  • 2,688
  • 6
  • 29
  • 61
  • Your document worked for me on MacOS, and showed the plot. What system are you on? What does `sessionInfo()` show? – user2554330 Jul 01 '19 at 16:45
  • It also worked for me when I tried it in Windows. So I'm guessing the problem is something specific to your system. – user2554330 Jul 01 '19 at 20:08
  • I am using Linux Mint 19... – antecessor Jul 01 '19 at 20:52
  • You probably haven't got the necessary libraries installed for rgl. It needs "OpenGL, GLU Library, XQuartz (on OSX), zlib (optional), libpng (>=1.2.9, optional), FreeType (optional), pandoc (>=1.14, needed for vignettes)". You appear to be missing FreeType and libpng. – user2554330 Jul 01 '19 at 21:30

1 Answers1

0

Sometimes you can use rgl.postscript() to create a PDF file from an rgl image. When it works, it will give the best quality, but it doesn't always work: transparency isn't supported, etc.

rgl.snapshot() will generate a PNG image of the current rgl window. That's likely to look a little pixellated unless you can make your rgl window really large. (It would be nice to have virtual windows bigger than your screen, but rgl doesn't support non-screen buffers, and things like Xvfb tend not to support OpenGL drawing.)

Those are used by knitr hooks that rgl can install, so you can have a document like this:

```{r echo=FALSE}
library(rgl)
library(car)
setupKnitr()
```

Here's a plot:

```{r rgl=TRUE, dev="png"}
scatter3d(mpg~hp+qsec|factor(cyl), data=mtcars, surface=FALSE, residuals=TRUE,
          parallel=FALSE, bg="white", axis.scales=TRUE, grid=TRUE, ellipsoid=TRUE)    
```

You can play with the chunk option dpi (dots per inch, default 72), but there will be an upper limit supported on your display. You should be able to get bigger than the default unless you have a really large figure.

user2554330
  • 37,248
  • 4
  • 43
  • 90