simple_plots.R

IanMac — Feb 4, 2013, 6:41 PM

#BASIC PACKAGE

par(mfrow=c(1,1)) #plot area: 1 row, 1 column

x <- rnorm(100, 10, 2) # create data
hist(x) # first plot, a histogram

plot of chunk unnamed-chunk-1


y  <- x + rnorm(100)
par(mar = c(2,2,2,2)) # not enough room for labels
plot (x,y, xlab='Weight', ylab='Height', main = 'Weight & Height')

plot of chunk unnamed-chunk-1


par(mar = c(4,4,2,2)) # now can see labels
par(mfrow=c(1,2)) #plot area: 1 row, 2 columns
plot (x,y, xlab='Weight', ylab='Height')
title ('Graph 3') # add title (same as 'main' in original args)
text(0,-2,'Label') # add label at (x = 0, y = -2)
legend('topright', legend='Data', pch =1) # add legend with symbol
fit <- lm(y ~ x) # linear model
abline(fit, lwd=3, col='red') # adds linear model line, thickness 5

plot(x,y,pch=2) #points type 2 - triangles

plot of chunk unnamed-chunk-1

plot(x,y,pch=22, bg='green', col='red') #points type 22 - boxes with fill and border

#creating different points on same graph
g <- gl(2, 50, labels = c('Male', 'Female')) # group with 2 levels, 50 elements in each
plot (x,y, type='n') #create plot without adding data
points(x[g == 'Male'], y[g == 'Male'], col='green', pch =2) #first add Male points
points(x[g == 'Female'], y[g == 'Female'], col='blue', pch=3) #nex add Female points

plot of chunk unnamed-chunk-1


# LATTICE PACKAGE

library(lattice)
library(nlme)

xyplot(distance ~ age | Subject, data = Orthodont, type = 'b')

plot of chunk unnamed-chunk-1


x<- rnorm(100)
y<- x + rnorm(100,,0.5)
xyplot(y~x) #single plot

plot of chunk unnamed-chunk-1


f <- gl(2, 50, labels=c('Group1', 'Group2'))
xyplot(y ~ x | f) #now two plots

plot of chunk unnamed-chunk-1


xyplot(y~x|f, 
panel = function (x,y, ...) { #custom panel function
    panel.xyplot(x,y, ...) #plot
    panel.lmline(x,y, col=2) #linear model line, red
    panel.abline(h=median(y), lty=2) # median line
    })

plot of chunk unnamed-chunk-1


#load environmental data frame
data(environmental)

xyplot(ozone ~ radiation, data = environmental) #basic plot

plot of chunk unnamed-chunk-1


temp.cut<- equal.count(environmental$temperature,4) #create 4 ranges of temperature
xyplot (ozone ~ radiation | temp.cut, data = environmental, layout = c(2,2), as.table = TRUE) #2 variables conditioned on a 3rd

plot of chunk unnamed-chunk-1


xyplot (ozone ~ radiation | temp.cut, data = environmental, layout = c(2,2), as.table = TRUE, pch = 22, #with custom panel
panel = function(x,y,...){ #add custom panel with various annotations
    panel.xyplot(x,y,...)
    fit <- lm(y~x)
    panel.abline(fit, col='green', lwd=3) #fitted line
    panel.loess(x,y, col='red', lwd=3) #smooth line
    }, xlab='Solar Radiation', ylab = 'Ozone (ppb)', #adding more args after the panel function
    main = "Ozone v's Solar Radiation") 

plot of chunk unnamed-chunk-1


#as above, but now with temp and wind ranges
temp.cut<- equal.count(environmental$temperature, 4) #create 4 ranges of temperature
wind.cut <- equal.count(environmental$wind, 4) #create 4 ranges of wind

xyplot (ozone ~ radiation | temp.cut  *  wind.cut, data = environmental, layout=c(4,4), as.table = TRUE, pch = 20, #with custom panel
panel = function(x,y,...){ #add custom panel with various annotations
    panel.xyplot(x,y,...)
    fit <- lm(y~x)
    panel.abline(fit, col='green', lwd=3) #fitted line
    panel.loess(x,y, col='red', lwd=3) #smooth line
    }, xlab='Solar Radiation', ylab = 'Ozone (ppb)', #adding more args after the panel function
    main = "Ozone v's Solar Radiation") 

plot of chunk unnamed-chunk-1


splom(~ environmental) #makes scatterplots of all data combinations

plot of chunk unnamed-chunk-1

histogram(~ ozone | temp.cut * wind.cut, data=environmental) #histogram

plot of chunk unnamed-chunk-1


#Mathematical annotations
#first example of math notation
plot(0,0, main = expression(theta=0),
    ylab = expression(hat(gamma) == 0),
    xlab = expression(sum(x[i] * y[i], i == 1 , n)))

#with some data
x <- rnorm(100)
hist(x, 
xlab = expression("The mean ("* bar(x) * ") is " *
sum(x[i]/n, i==1, n)))

plot of chunk unnamed-chunk-1


#with text and derived values
plot (x, y,
xlab = substitute('The average value for x: ' * bar(x) == k, list (k = mean(x))),
ylab = substitute('The average value for y: ' * bar(y) == k, list (k = mean(y))))

# in a loop of plots
par(mfrow = c(2,2))

plot of chunk unnamed-chunk-1

for (i in 1:4) {
    x<- rnorm (100)
    hist(x, main = substitute (theta == num, list(num = i)))
    }

plot of chunk unnamed-chunk-1


hist(x, main=expression('Partial Differential Annotation:  '*partialdiff*'y/'*partialdiff*'x'))
hist(x, main=expression('Integral Annotation:  '*integral(f(x)*dx,0,infinity)))

plot of chunk unnamed-chunk-1