Groupwise comparison for continuous variables

Load BONEDEN.DAT.txt as bone. (Bone density in smoking-dose-discordant twins)

bone <- read.csv("BONEDEN.DAT.txt", quote = "'")
head(bone)
       ID age zyg ht1 wt1 tea1 cof1 alc1 cur1 men1 pyr1  ls1  fn1  fs1 ht2 wt2 tea2 cof2 alc2 cur2 men2  pyr2  ls2
1 1002501  27   2 162  57   35    0    1    1    0    0 0.81 0.72 1.00 160  56   42   21    0    0    0 13.75 0.76
2 1015401  42   2 165  76   42    2    3    5    1    0 1.01 0.74 0.99 159  72   20   21    1    1    0 48.00 0.89
3 1027601  59   2 150 114   12    0    0    0    1    0 0.75 0.63 1.05 156  54    7   28    0    0    1 20.50 0.51
4 1034301  61   1 159  62   56    0    0    0    1    0 0.81 0.64 1.12 162  58   21   35    0    0    1 29.75 0.85
5 1121202  47   2 159  58   28   14    0    0    0    0 0.78 0.70 1.14 150  58   91    0    0    1    1 25.00 0.59
6 1162502  33   2 163  49   35    0    3    1    0    0 0.86 0.56 1.13 158  54   14   14    0    0    0  5.00 0.83
   fn2  fs2
1 0.68 1.04
2 0.64 1.11
3 0.64 0.86
4 0.69 1.03
5 0.54 0.96
6 0.50 1.06

Load BETACAR.DAT.txt as vitA (4-types of beta-carotene supplements and plasma carotene concentration)

vitA <- read.csv("BETACAR.DAT.txt", quote = "'")
head(vitA)
  Prepar Id Base1lvl Base2lvl Wk6lvl Wk8lvl Wk10lvl Wk12lvl
1      1 71      298      116    174    178     218     190
2      1 73      124      146    294    278     244     262
3      1 80      176      200    276    286     308     334
4      1 83      116      180    164    238     308     226
5      1 90      152      142    290    300     270     268
6      1 92      106      106    246    206     304     356

Load lattice and ggplot2 packages

library(lattice)
library(ggplot2)

Histograms (different packages give different defaults)

## hist() is the basic function.
hist(bone$age)

plot of chunk unnamed-chunk-5


## lattice::histogram() gives a fancier version
histogram(bone$age)

plot of chunk unnamed-chunk-5


## ggplot2's geom = "histogram"
qplot(x = age, data = bone, geom = "histogram")

plot of chunk unnamed-chunk-5

Density plot. lattice densityplot is probably most useful.

## base
plot(density(bone$age))

plot of chunk unnamed-chunk-6


## lattice
densityplot(bone$age)

plot of chunk unnamed-chunk-6


## ggplot2
qplot(x = age, data = bone, geom = "density")

plot of chunk unnamed-chunk-6


Scatter plot

## base
plot(fn1 ~ age, bone)

plot of chunk unnamed-chunk-7


## lattice
xyplot(fn1 ~ age, bone)

plot of chunk unnamed-chunk-7


## ggplot2
qplot(age, fn1, data = bone)

plot of chunk unnamed-chunk-7

Box plot

## base
boxplot(Wk12lvl ~ Prepar, vitA)

plot of chunk unnamed-chunk-8


## lattice
bwplot(Wk12lvl ~ factor(Prepar), vitA)

plot of chunk unnamed-chunk-8


## ggplot2
qplot(factor(Prepar), Wk12lvl, data = vitA, geom = "boxplot")

plot of chunk unnamed-chunk-8


Grouped scatter plot

## base
layout(matrix(1:4,ncol = 2))
for (i in 1:4) {
        plot(Wk12lvl ~ Base1lvl, subset(vitA, Prepar == i))
        title(paste("Prepar = ", i))
}

plot of chunk unnamed-chunk-9


## lattice
xyplot(Wk12lvl ~ Base1lvl | factor(Prepar), vitA)

plot of chunk unnamed-chunk-9


## ggplot2
ggplot(vitA, aes(x = Base1lvl, y = Wk12lvl, group = factor(Prepar))) + geom_point() + facet_wrap(~Prepar)

plot of chunk unnamed-chunk-9