Translate

Wednesday, 14 October 2015

3D Graph - Lattice Package



cloud() function in 'lattice' package provides 3-dimensional visualisation option

library(lattice)

cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species)
  






















  
  

  
Adding a title
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species")  
 























  


Modify axis  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5),y=list(cex=0.9,tck=0.5),z=list(cex=0.9,tck=0.5)))
  






















  
  


Modify frame style  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1")))
























  
  
Modify axis labels  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)))
    























      




Modify plotting style  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"))
























  



Insert legend/key  
  
cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="Iris Species",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),key=list(space="right",title="Species",cex.title=1,points=list(col=c("tan","goldenrod","brown"),cex=0.9,pch=16),text=list(lab=levels(iris$Species),cex=0.9)))
  






















  
  




Rotating the graph (displayed using 'gridExtra' package for better comparison):
  
library(gridExtra)  
    
- around x-axis  
#default g1<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="x = -60 (default)",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=-60,y=0))

#x rotates graph around x axis - i.e. moves viewing  angle up and down - e.g. for view from top (x=0) g2<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="x = 0",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=0))

g3<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="x = -30",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=-30,y=0))


grid.arrange(g1,g3,g2,nrow=1,main="Rotating Around X-Axis")

  



























  

- around z-axis  
  
#z rotates graph around z axis - i.e. moves viewing  angle left and right - e.g. for view from front (z=0) 
g1<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="z = 40 (default)",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=-60,y=0))

g2<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="z = 20",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=20,x=-60,y=0))

g3<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="z = 0",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=0,x=-60,y=0))

grid.arrange(g1,g2,g3,nrow=1,main="Rotating Around Z-Axis")
  























  
- around y-axis  
  
#y rotates graph around y axis - i.e. moves viewing  angle up and down but hinges on y-axisg1<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="y = 0 (default)",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=0))

g2<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="y = 30",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=30))

g3<-cloud(Sepal.Length~Sepal.Width*Petal.Length,iris,pch=16,alpha=0.7,cex=1,groups=iris$Species,main="y = 60",scales=list(arrows=FALSE,x=list(cex=0.9,tck=0.5,col="wheat3"),y=list(cex=0.9,tck=0.5,col="wheat3"),z=list(cex=0.9,tck=0.5,col="wheat3")),par.settings=list(box.3d=list(col="wheat1"),par.xlab.text=list(cex=0.8,col="tan3"),par.ylab.text=list(cex=0.8,col="tan3"),par.zlab.text=list(cex=0.8,col="tan3",rot=90)),col=c("tan","goldenrod","brown"),screen=list(z=40,x=0,y=60))

grid.arrange(g1,g2,g3,nrow=1,main="Rotating Around Y-Axis")


  

























For 3D-bar/column chart:


library(latticeExtra)

Dat <- aggregate(Sepal.Length~Species+Petal.Length, iris, mean)

cloud(Sepal.Length ~ Species + Petal.Length, Dat, panel.3d.cloud = panel.3dbars,
               col.facet=colorRampPalette(c("dodgerblue", "salmon"))(nrow(Dat)),
                xbase = 0.4, ybase = 0.4, scales = list(arrows = FALSE, col = 1),
                col = "white", par.settings = list(axis.line = list(col = "transparent"))
        )













No comments:

Post a Comment