Graphing
  with
 2012-11-19 @HSPH
Kazuki Yoshida, M.D.
  MPH-CLE student


                       FREEDOM
                       TO	
  KNOW
Group Website is at:
http://rpubs.com/kaz_yos/useR_at_HSPH
Previously in this group
n   Introduction

n   Reading Data into R (1)

n   Reading Data into R (2)

n   Descriptive, continuous

n   Descriptive, categorical




                    Group Website: http://rpubs.com/kaz_yos/useR_at_HSPH
Menu


n   Basic graphing with R
Ingredients
          Statistics                  Programming
n   One-variable plot         n   base, lattice, and ggplot2

     n   Histogram            n   hist(), histogram()

     n   Density plot         n   densityplot()

n   Two-variable plot         n   plot(Y~X)

     n   Scatter plot         n   boxplot(), bwplot()

     n   Box plot             n   Conditioning in lattice

n   Grouped plot
3 different systems
base (traditional)




        http://commons.wikimedia.org/wiki/File:Plottype.png
lattice package




    http://zoonek.free.fr/blosxom/R/2006-08-10_R_Graphics.html
ggplot2




http://blog.revolutionanalytics.com/2009/09/ggplot2-and-the-grammar-of-graphics.html
Open
R Studio
Install and Load
 lattice ggplot2
Download comma-separated and Excel




 Put them in folder
BONEDEN.DAT.txt
BETACAR.DAT.txt
        http://www.cengage.com/cgi-wadsworth/course_products_wp.pl?
                 fid=M20bI&product_isbn_issn=9780538733496
Read in BONEDEN.DAT.txt
       Name it bone

 Bone density in twins with
discordant smoking exposure
Read in BETACAR.DAT.txt
            Name it vitA

Plasma level of carotene by different
     formula of beta-carotene
formula



    Y-axis ~ X-axis
Histogram
## base
hist(bone$age)

## lattice
histogram(bone$age)

## ggplot2
qplot(x = age, data = bone, geom = "Histogram")
Density plot
## base
plot(density(bone$age))

## lattice
densityplot(bone$age)

## ggplot2
qplot(x = age, data = bone, geom = "density")
Scatter plot
## base
plot(fn1 ~ age, bone)

## lattice
xyplot(fn1 ~ age, bone)

## ggplot2
qplot(age, fn1, data = bone)
Box plot
## base
boxplot(Wk12lvl ~ Prepar, vitA)

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

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

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

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

(Very) Basic graphing with R