Spinning 3D Scatterplot in R - Crashes - r

I'm trying to create a 3D rotating scatterplot in R using the package Rcmdr:
ParticlePos=read.table('FILE.dat',header=T,fill=T)
library(Rcmdr)
attach(mtcars)
scatter3d(ParticlePos[,1],ParticlePos[,2],ParticlePos[,3],xlab=expression(x),ylab=expression(y),zlab=expression(z),pch=19, col="red", size=3)
but it keeps crashing. It seems that it's opening up an XQuartz (v. 2.7.4, OS X 10.8) window and plotting it correctly, but after about a half second it closes leaving me with nothing. I'm fairly certain all of the appropriate dependencies and such are downloaded.
Would anyone have any ideas?
Thanks!

Related

3D RGL Plot not showing in R

I am plotting a 3 dimensional PCA plot with R package "pca3d". However, the plot is not popping up as it usually does with conventional 2d plots (e. g. plotted with ggplot, ggbiplot or also with the 2d function of pca3d, i. e. pca2d).
> library(pca3d)
> pca3d(prncomp.pca)
[1] 0.4933764 0.3238404 0.3194022
The above is all I get. I guess pca3d relies on RGL. I saw this other thread Why is my 3D plot not showing up in R Studio plot viewer? bu was not able to solve my problem yet. Any suggestions?
EDIT: I am working on ubuntu 18.04

Creating plot using R under Mac OS

I use R in Mac through command line. When I need to draw some plot, R open plotting device called Quartz to display plot. It all works fine, but problem arise when I update a plot or draw a new plot based on changed plot. It looks like Quartz device does not automatically update the same. I need to close Quartz before I update my data and hit enter, so that new/modified plot can appear in Quartz.
I am wondering if this is default behaviour when R is run from command line in Mac? Is there any way to automatically update the plot in Quartz every time I update the data for plotting?
Any pointer will be highly appreciated.

Saving filled contour plots to PDF in R gives pixelated results

Saving the output of one of R's built-in examples for the filled.contour function to PDF:
pdf('test.pdf')
require("grDevices")
filled.contour(volcano, asp = 1)
dev.off()
produces a discretised result (see below). Is there any way to fix this? System info:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS
Edit after the accepted answer:
The problem is actually reported in the documentation of the pdf function:
If you see problems with PDF output, do remember that the
problem is much more likely to be in your viewer than in R ...
Symptoms for which the viewer has been at fault are apparent
grids on image plots ...
Unfortunately the default viewers on most Linux and macOS
systems have these problems, and no obvious way to turn off
graphics anti-aliasing.
It's an odd place to document the problem because the subdivision into rectangular segments is clearly caused by filled.contour and not by pdf. Otherwise ggplot2's output would also suffer from the same issue.
That is likely a result of antialiasing: when you display the image, it draws squares one at a time. As they are being drawn, the edge is a mix of the square colours and the white background, so it is drawn lighter.
Unfortunately, this isn't something that's really under your control. It's the PDF previewer that is introducing the artifacts. See this page https://codedocean.wordpress.com/2014/02/03/anti-aliasing-and-image-plots/ for a discussion.
The recommendation there worked for me: use the png() device with type = "cairo". This gives bitmapped output rather than the vector output of a pdf().
png('test.png',type="cairo")
filled.contour(volcano, asp = 1)
dev.off()
Edited to add:
I don't think you can do better with filled.contour, but if you are willing to switch to ggplot2 graphics you can. When it draws filled contours it appears to do it using polygons, not the image style plot that filled.contour uses. This still shows the same antialiasing bugs in the previewer, but now the lines appear along the borders between colours, which is much less irritating. For example:
df <- data.frame(x = as.numeric(row(volcano)-1)/(nrow(volcano)-1),
y = as.numeric(col(volcano)-1)/(ncol(volcano)-1),
z = as.numeric(volcano))
pdf('test.pdf')
library(ggplot2)
ggplot(df, aes(x=x, y=y, z=z)) +
geom_contour_filled()
dev.off()
I don't know how to get the same palette as filled.contour uses, i.e. function(n) hcl.colors(n, "YlOrRd", rev = TRUE). Maybe someone else can show us in a comment.

R Programming on plot to label the axes

Create a plot with cars dataset and attribute speed along the X axis and distance in y axis .
Label the axes.
I have done this programming still there is some problem:
plot(cars,xlab="speed",ylab="distance")
Even though everything seems correct the terminal doesn't move to the next part
What environment are you using? I was able to execute the following code and produce the plot you want using both RStudio and R directly in the terminal.
library(MASS)
plot(cars, xlab="Distance", ylab="Speed")
If you're running straight from the terminal then you'll need to specify a window to pop up. The following question has an answer that outlines what to do depending on your operating system:
How to pop up the graphics window from Rscript?
So, for example, if you don't have your system configured to automatically open the plot window, and you're running on a mac, the following code will produce what you want directly from running R in the terminal:
library(MASS)
X11()
plot(cars, xlab="Distance", ylab="Speed")

plot3D from rgl package doesn't appear

I'm trying to use the rgl package to produce 3D plots of my graphs in RStudio. I use a Mac and I have XQuartz installed, but when I follow step-by-step tutorials for 3D plotting even the simplest plots won't work.
No warning message appears and no plot appears after I launch the command. When I bring all windows forward I can see a small one with no close/expand button and no images on it (same goes for the plot area of the workspace, nothing happens at all).
Does anybody have an idea of why this could be the case? I updated R a couple of weeks ago and all of my packages have been uninstalled and re-installed.
Thanks!
Here's the basic code I used, just in case.
x=runif(1000)
y=runif(1000)
z=rnorm(1000)
install.packages("rgl")
library("rgl")
plot3d(x,y,z)

Resources