Showing posts with label regression line. Show all posts
Showing posts with label regression line. Show all posts

Monday, April 8, 2013

RG#36: Multiple scatter plots of trallis type

 set.seed(1234)
X <- rnorm (480, 10,5)
myd <- data.frame (X1 = rep (c("A", "B", "C", "D"), each = 120), X2 = rep (rep(rep(c("E", "F"), 4), each = 30), 4), X3 = X, Y = X*0.5 + rnorm (length (X), 0, 4)) 


# need ggplot2
library(ggplot2)



qplot(X3, Y, data=myd, facets=X1~X2, xlab="X1 (units)", ylab="Y1 (units)") + theme_bw()      

 


# add regression lines
qplot(X3, Y, data=myd, facets=X1~X2, xlab="X1 (units)", ylab="Y1 (units)", geom=c("point", "smooth"), method="lm", formula= y~x) + theme_bw()






 

Friday, April 5, 2013

RG #4: basic XY plot with regression line (with diffrent plotting parameters) in R

# data
length <- c(10.2, 35, 16.3, 8.9, 14.2, 20.1, 4.3)
width <- c(3.2, 1.5, 2.3, 4.9, 16.2, 3.1, 5.3)


# simple scatter plot
plot(length, width)

# adding regression line
 abline(lm(width ~ length))
# add title
title("Regression of length on width")



# with different parameters, see ?par to see what they mean
  plot(length, width, type ="b" , lty = 2, main = " main title", sub = " sub title", xlab = " x variable (units) ", ylab = " y variable units ", bg = "white", bty= "7", cex= 2, cex.axis = 1,
cex.lab = 1, cex.main = 2, cex.sub = 1, col = "red", col.axis = "black", col.lab = "black",
col.main = "blue", col.sub = "black", family = "sans", fg = "black", font = 3, font.axis = 3,
font.main = 2, font.sub =1, pch = 19, tck = 1 )




# adding grid lines
 grid(nx = 40, ny = 40, col = "lightgray", lty = "dotted", lwd = par("lwd"), equilogs = TRUE)


# adding regression line
abline(lm(width ~ length))

# changing box:
box(lty = '1373', col = 'blue')



 

RG #3: multiple scatter plot with smoothed line (trellis plots)




# data 
set.seed (12345)
y1 <- rnorm(1000, 12,9)
x1 <- 0.35 *y1 + rnorm(1000, 0,3)
x2 <-  c(rep("A",500), rep("B",500))
x3 <-  c(rep(1,100), rep(2,100), rep(3,100), rep(4,100), rep(5, 100), rep(1,100), rep(2,100), rep(3,100), rep(4,100), rep(5, 100))
data2 <- data.frame(x1, y1, x2, x3)


require(ggplot2) 
# scatter plots with out lines 
qplot(y1, x1, data =data2) + facet_grid(x2 ~ x3)+theme_bw()

# with smoothed lines  
qplot(y1, x1, data =data2, geom = c("point", "smooth"), method = "lm") + facet_grid(x2 ~ x3)+ theme_bw()