0

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?

BillyJean
  • 1,537
  • 1
  • 22
  • 39
  • 1
    plot_func returns the legend3d output only, and the two elements are not the same – moodymudskipper May 30 '17 at 15:12
  • @Moody_Mudskipper How do I make `plot_func` return the combined plot? – BillyJean May 30 '17 at 15:13
  • I don't know if you can store these plots as object, the function plot3d draws a plot, it doesn't store anything in an object. If you work with ggplot2 you'll be working with plot objects. – moodymudskipper May 30 '17 at 15:18
  • check also lattice package, see the second answer here: https://stackoverflow.com/questions/1816480/generating-names-iteratively-in-r-for-storing-plots – moodymudskipper May 30 '17 at 15:27

0 Answers0