Friday, March 29, 2013

Session # 10 - March 26, 2013 -- Plotting in R

IT BAL Assignment#10

Question 1: 

Create 3 vectors, x, y, z and choose any random values for them, ensuring they are of equal length, bind them together.Create 3 dimensional plots of the same.

Solution:

First creating a random data set of 50 items with mean =30 and standard deviation =10

> data <- rnorm(50,mean=30,sd=10)
> data

Taking sample data of length 10 from the created data set in three different vectors x,y,z
> x <- sample(data,10)
> x

> y <- sample(data,10)
> y

> z <- sample(data,10)
> z

Binding the three vectors x,y,z into a vector T using cbind
> T <- cbind(x,y,z)
> T

                                                                       Data Set
Plotting 3d graph 

Command:

> plot3d(T[,1:3])
                                                                        3D Plot
Plotting of graph with labels for axes and color

Command 
> plot3d(T[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(500))
 
                                                                      3D Plot with color
Plotting of graph with labels for axes, color and type = spheres

Command
> plot3d(T[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(5000), type='s')
                                                                      3D Plot with spheres
Plotting of graph with labels for axes, color and type = points

Command

> plot3d(T[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(5000), type='p')
                                                                   3D Plot with points
Plotting of graph with labels for axes, color and type = lines

Command

> plot3d(T[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(5000), type='l')
                                                                     3D Plot with lines
Question2 :


Choose 2 random variables 
Create 3 plots: 
1. X-Y 
2. X-Y|Z (introducing a variable z and cbind it to z and y with 5 diff categories)
3. Color code and draw the graph 
4. Smooth and best fit line for the curve

Solution

Creating a data set for two random variables and then introducing third variable z

Command:

> x <- rnorm(5000, mean= 20 , sd=10)
> y <- rnorm(5000, mean= 10, sd=10)
> z1 <- sample(letters, 5)
> z2 <- sample(z1, 5000, replace=TRUE)
> z <- as.factor(z2)
> z
                                                                      Data Set 
Creating Quick Plots

Command:

>qplot(x,y)
                                                                     x and y qplot
>qplot(x,z)
                                                                          x and z qplot
For semi-transparent plot

> qplot(x,z, alpha=I(2/10))
                                                                    Semi-transparent Plot
For coloured plot

> qplot(x,y, color=z)
                                                                      Coloured plot
For Logarithmic coloured plot

> qplot(log(x),log(y), color=z)
                                                                       Logarithmic Plot
Best Fit and Smooth curve using "geom"

Command:

> qplot(x,y,geom=c("path","smooth"))
                                                                         geom='path'

> qplot(x,y,geom=c("point","smooth"))
 
geom='point'
> qplot(x,y,geom=c("boxplot","jitter"))
                                                                geom='boxplot' and 'jitter'

No comments:

Post a Comment