Your verification ID is: guDlT7MCuIOFFHSbB3jPFN5QLaQ Big Computing: multicolored scatter plot in R
Showing posts with label multicolored scatter plot in R. Show all posts
Showing posts with label multicolored scatter plot in R. Show all posts

Tuesday, October 7, 2014

Plotting in R (scatter plot, multicolor and linear regression) Part 2

Also we may want to plot points on a graph where the points are of different classes or groups. Below I create a data set with four variable that all have a linear relationship to each other.
group<-data.frame()
indep<-runif(900,0,10)
dep1<-5*indep[1:300]+rnorm(300,3,3)
dep2<-7*indep[301:600]+rnorm(300,2,6)
dep3<-9*indep[601:900]+rnorm(300,1,7)
deps<-c(dep1,dep2,dep3)
group[1:300,1]<-1
group[301:600,1]<-2
group[601:900,1]<-3
groups<-cbind(indep,deps,group)
When we plot without speerating the groups it looks like there might be a linear relationship. However, if you look closely you will see that the plot is slightly cone shaped which should tell you there is something else going on.
plot(deps~indep,groups)
plot of chunk unnamed-chunk-4
Now lets plot the same data except each group is plotted as a different color
plot(deps~indep,groups,col=c("red","yellow","blue")[V1])
plot of chunk unnamed-chunk-5
This make it easy to see we have three different groups that all have a linear relationship. we can even plot them adding the regression line for each one.
plot(deps~indep,groups,col=c("red","yellow","blue")[V1])
abline(lm(deps[1:300]~indep[1:300],groups),lwd=2)
abline(lm(deps[301:600]~indep[301:600],groups),lwd=3)
abline(lm(deps[601:900]~indep[601:900],groups),lwd=4)
plot of chunk unnamed-chunk-6
Next time I will do pairs plots in base r which is really useful to look at the relationship of each variable to another.