I want to generate around 50 3-D plots in R based on data in a list. These plots I will feed to grid.arrange
in order to plot them all on one page.
My issue is with the part of generating the plots. In the following example I show my current approach, and to keep it simple I only generate two plots:
library(rgl)
data<-list()
data[[1]] <- list("data" =data.frame("x" = 1*(1:10), "y" = 2*(1:10),"z" = 3*(1:10)),
"dataO"=data.frame("x" = 1, "y" = 2, "z" = 3))
data[[2]] <- list("data" =data.frame("x" = 4*(1:10), "y" = 5*(1:10),"z" = 6*(1:10)),
"dataO"=data.frame("x" = 4, "y" = 5, "z" = 6))
plot_func = function (data) {
plot3d(data$data, type='s', size=0.5)
points3d(data$dataO, color='red', size=10)
rgl.viewpoint( theta = -160, phi = 15, fov=60)
legend3d("topright",
legend = c('Data', 'Outliers'),
pch = 16,
col = c("black","red"),
cex = 1,
inset=c(0.02))
}
tmp<-lapply(data[c(1,2)], plot_func)
Problem with the code: The list indeed contains two plots as desired, however, the issue is that both entries contain the second plot that is based on data[[2]]
.
Is there a way to remedy this?