Feeds:
Posts
Comentários

Posts Tagged ‘plot’

There at least two functions to build a surface plot in R, persp() and wireframe(). Here I show a documented code to build a surface with wireframe() and how to define the color of each surface.

The code below generates this graph

# Required packages
library(lattice)

# Example: data format
#         x       y        z      group
# 1      1.0     0.5    1.5000    data1
# 2      1.0     0.6    1.6000    data1
# 3      1.0     0.7    1.7000    data1
# 4      1.0     0.8    1.8000    data1
# 90     1.4     0.6    6.9712    data2
# 91     1.4     0.7    7.0817    data2
# 92     1.4     0.8    7.2512    data2
# 93     1.4     0.9    7.4977    data2

# Generating the parameters
rm(list=ls())
x <- seq(1,2,0.2);
y <- seq(0.5,1.5,0.1);

data1 <- matrix(0,nrow=length(x)*length(y),ncol=3);
data2 <- matrix(0,nrow=length(x)*length(y),ncol=3);

n <- 0;
j <- 1;
while(j<=length(x)){
   for (k in 1:length(y)){
       data1[k+n,1] <- x[j];
       data1[k+n,2] <- y[k];
       data1[k+n,3] <- x[j]^4 + y[k];

       data2[k+n,1] <- x[j];
       data2[k+n,2] <- y[k];
       data2[k+n,3] <- x[j]^4 + y[k]^4 + 3;
   }
   n <- n+length(y);
   j <- j+1;
}
rm(x,y,j,n,k)

# Arranging data into a data frame
data1_2 <-as.data.frame(rbind(data1,data2));
colnames(data1_2) <- c("x","y","z");
data1_2$group <- gl(2, nrow(data1_2)/2, labels=c("data1", "data2"))
rm(data1,data2)

# Plotting data as a surface
wireframe(z~x*y,data=data1_2,groups=group,

      # Naming labels and Axis
      main =list(label="Plot of 2 Surfaces",cex=2,distance=5),
      zlab=list(rot=90,label = "Z",cex=2),
      xlab=list(label = "X",cex=2),
      ylab=list(label = "Y",cex=2),

      # Coloring the groups
      col.groups=c(rgb(red=255,green=153,blue=102,
                       alpha=200,maxColorValue=255),  # Orange
                   rgb(red=207,green=231,blue=245,
                       alpha=200,maxColorValue=255)), # Blue

      # Coloring the grids
      col=c(rgb(red=0,green=0,blue=0,alpha=50,maxColorValue=255),
            rgb(red=0,green=0,blue=0,alpha=50,maxColorValue=255)),

      aspect=c(1,1), # y-size/x-size and z-size/x-size
      screen = list(z=40,y=0,x=-80)); # axis rotation

Read Full Post »