[R] [External] Re: Save a graph file use jpeg(file=file)

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Jan 6 11:10:03 CET 2022


What Rich said will work.  Here are some details to think about:

There are two ways that snapshot3d works.  The original way takes a 
snapshot from your screen.  That will produce an image the same as what 
you see, at the same resolution.  You get this mode if you set "webshot 
= FALSE", or if you don't have the webshot2 package installed.

The newer way uses the webshot2 package.  Unfortunately that package is 
not on CRAN, so you'd need to install it using 
remotes::install_github("rstudio/webshot2") .  It requires that you have 
the Chrome browser installed (or supposedly some other browser providing 
the same libraries; I don't know what else will work).

The webshot2 package doesn't need to display on your screen, so in 
theory you can produce larger snapshots.  It can also do a better 
version of rendering:  the rgl window uses OpenGL 1.x methods, while 
webshot2 uses WebGL, which is based on a newer OpenGL version.  I find 
the font support to be better, and there are some improvements to 
shading of surfaces.  On the negative side, it can be much slower, 
especially on a complex scene, and occasionally has different bugs than 
the other display.

Finally, you don't need to use your mouse to change the window size; you 
can specify width and height in the snapshot3d() call, and the window 
will change as requested.

Duncan Murdoch


On 05/01/2022 8:32 p.m., Sorkin, John wrote:
> I'll try that.
> 
> Get Outlook for iOS <https://aka.ms/o0ukef>
> ------------------------------------------------------------------------
> *From:* Richard M. Heiberger <rmh using temple.edu>
> *Sent:* Wednesday, January 5, 2022 8:31:15 PM
> *To:* Sorkin, John <jsorkin using som.umaryland.edu>; Duncan Murdoch 
> <murdoch.duncan using gmail.com>; r-help using r-project.org (r-help using r-project.org) 
> <r-help using r-project.org>
> *Subject:* Re: [External] Re: [R] Save a graph file use jpeg(file=file)
> you can make the rgl window full screen, and then stretch the graph to 
> fill the window, and then use snapshot 3d
> ------------------------------------------------------------------------
> *From:* R-help <r-help-bounces using r-project.org> on behalf of Sorkin, John 
> <jsorkin using som.umaryland.edu>
> *Sent:* Wednesday, January 5, 2022 8:23:34 PM
> *To:* Duncan Murdoch <murdoch.duncan using gmail.com>; r-help using r-project.org 
> (r-help using r-project.org) <r-help using r-project.org>
> *Subject:* [External] Re: [R] Save a graph file use jpeg(file=file)
> Ducan,
> 
> As always, you are very giving of your time to help R users. Thank you!
> 
> scatter3d is from the car package.
> 
> As I understand snapshot3d, it takes a screen shot and saves the 
> resultant file. This implies that the resultant file will be 72 DPI. I 
> need to get a higher resolution image, at least 300 DPI. Do you have any 
> suggestions?
> 
> Thanks as always.
> 
> John
> 
> ________________________________________
> From: Duncan Murdoch <murdoch.duncan using gmail.com>
> Sent: Wednesday, January 5, 2022 2:58 PM
> To: Sorkin, John; r-help using r-project.org (r-help using r-project.org)
> Subject: Re: [R] Save a graph file use jpeg(file=file)
> 
> On 05/01/2022 2:45 p.m., Sorkin, John wrote:
>> I am trying to create a 3-D graph (using scatter3d) and save the graph to a file so I can insert the graph into a manuscript. I am able to create the graph. When I run the code below an RGL window opens that has the graph. The file is saved to disk after  dev.odd() runs. Unfortunately, when I open the saved file, all I see 
> is a white window. Can someone tell me how to have the file so I can 
> subsequently read and place the file in a paper? The problem occurs 
> regardless of the format in which I try to save the file, e.g. png, tiff.
>>
>>
>> x <- 1:10
>> y <- 2:11
>> z <- y+rnorm(10)
>> ForGraph<-data.frame(x=x,y=y,z=z)
>> ForGraph
>>
>> gpathj <- file.path("C:","LAL","test.jpeg")
>> gpathj
>> jpeg(file = gpathj)
>> par(mai = c(0.5, 0.5, 0.5, 0.5))
>> scatter3d(z=ForGraph$x,
>>            y=ForGraph$y,
>>            x=ForGraph$z,
>>            surface=FALSE,grid=TRUE,sphere.size=4
>>            ,xlab="Categories",ylab="ScoreRange",
>>            zlab="VTE Rate (%)",axis.ticks=TRUE)
>> dev.off()
>>
>>
> 
> You didn't say what package you found scatter3d in, but you did say it
> opens an rgl window.  rgl doesn't use R graphics devices, so jpeg() and
> the like don't work.
> 
> To save an rgl image, you use the rgl::snapshot3d() function.  It only
> has support for saving to PNG format.
> 
> If you are writing your manuscript in R Markdown or knitr Rnw, there are
> ways to have the file included automatically.  There have been methods
> to use with Sweave as well, but those aren't being maintained:  people
> should use knitr instead.
> 
> Duncan Murdoch
> 
>          [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7Crmh%40temple.edu%7C7efac70023f74f426dda08d9d0b337b8%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637770290874513198%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=QjooPjMvPZ%2BTnPh%2B9SOISOyeGVRYFMmU2NSfkel7rug%3D&reserved=0 
> <https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7Cjsorkin%40som.umaryland.edu%7C1de6e94adaff4ee3df8908d9d0b43bd2%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637770294833471347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=ZzC0nzJzm5XlBE1eb0oMmwcy31%2FjDQSySkspobqGXto%3D&reserved=0>
> PLEASE do read the posting guide 
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Crmh%40temple.edu%7C7efac70023f74f426dda08d9d0b337b8%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637770290874513198%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=TCjwf2%2FaEiUl6d%2BUx37bRRQOsXjUx80DhvwKbeCnkHg%3D&reserved=0 
> <https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Cjsorkin%40som.umaryland.edu%7C1de6e94adaff4ee3df8908d9d0b43bd2%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637770294833471347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=dqIeUcRjrWKCuRlmR%2FYwmTn2OIgNYLQHnnU%2FBSwK82Q%3D&reserved=0>
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list