Saturday, 30 March 2013

Business Application IT Lab--Plotting in R



IT Business Application Lab Assignments#10

Session 10
Date:  26th March,2013



Assignment 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:


Step 1: Creating a random data set of 50 items with mean =30 and standard deviation =10

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

Step 2:

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

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

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

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

Output:




Plotting of 3 dimensional graphs:

Command:

plot3d(c[,1:3])

Output:



Plotting of graph with labels for axis and colors

Command:

> plot3d(c[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(500)) 

Output:



Plotting of graph with labels for axis and colors and type "Spheres"

Command;

> plot3d(c[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(500),type="s")

Output:



Plotting of graph with labels for axis and colors and type "Points"

Command:

> plot3d(c[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(500),type="p")

Output:



Plotting of graph with labels for axis and colors and type "Line"

Command:

> plot3d(c[,1:3], xlab="X Axis" , ylab="Y Axis" , zlab="Z Axis", col=rainbow(500),type="l")

Output:




Assignment 2:


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:

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

Output:



Creating Quickplots

Command:

>qplot(x,y)

Output:


Command:

>qplot(x,z)

Output;


Creating Semi-Transparent plot

Command:


> qplot(x,z, alpha=I(2/10))


Output:


Creating Colored plot

Command:

> qplot(x,y, color=z)

Output:


Creating Logarithmic Color plot

Command:

> qplot(log(x),log(y), color=z)

Output:


Best fit and smooth curve using "geom"

Command:

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

Output:


Command:

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

Output:


Command:

> qplot(x,y,geom=c("boxplot","jitter"))

Output:





No comments:

Post a Comment