(very) basic graphing with r

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

Upload: kazuki-yoshida

Post on 10-May-2015

2.009 views

Category:

Education


1 download

TRANSCRIPT

Page 1: (Very) Basic graphing with R

Graphingwith

2012-11-19 @HSPHKazuki Yoshida, M.D. MPH-CLE student

FREEDOMTO  KNOW

Page 2: (Very) Basic graphing with R

Group Website is at:

http://rpubs.com/kaz_yos/useR_at_HSPH

Page 3: (Very) Basic graphing with R

n Introduction

n Reading Data into R (1)

n Reading Data into R (2)

n Descriptive, continuous

n Descriptive, categorical

Previously in this group

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

Page 4: (Very) Basic graphing with R

Menu

n Basic graphing with R

Page 5: (Very) Basic graphing with R

Ingredients

n One-variable plot

n Histogram

n Density plot

n Two-variable plot

n Scatter plot

n Box plot

n Grouped plot

n base, lattice, and ggplot2

n hist(), histogram()

n densityplot()

n plot(Y~X)

n boxplot(), bwplot()

n Conditioning in lattice

Statistics Programming

Page 6: (Very) Basic graphing with R

3 different systems

Page 7: (Very) Basic graphing with R

base (traditional)

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

Page 8: (Very) Basic graphing with R

lattice package

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

Page 10: (Very) Basic graphing with R

Open R Studio

Page 11: (Very) Basic graphing with R

lattice ggplot2Install and Load

Page 13: (Very) Basic graphing with R

Read in BONEDEN.DAT.txt

Name it bone

Bone density in twins with discordant smoking exposure

Page 14: (Very) Basic graphing with R

Read in BETACAR.DAT.txt

Name it vitA

Plasma level of carotene by different formula of beta-carotene

Page 15: (Very) Basic graphing with R

Y-axis ~ X-axis

formula

Page 16: (Very) Basic graphing with R

Histogram

Page 17: (Very) Basic graphing with R

## basehist(bone$age)

## latticehistogram(bone$age)

## ggplot2qplot(x = age, data = bone, geom = "Histogram")

Page 18: (Very) Basic graphing with R

Density plot

Page 19: (Very) Basic graphing with R

## baseplot(density(bone$age))

## latticedensityplot(bone$age)

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

Page 20: (Very) Basic graphing with R

Scatter plot

Page 21: (Very) Basic graphing with R

## baseplot(fn1 ~ age, bone)

## latticexyplot(fn1 ~ age, bone)

## ggplot2qplot(age, fn1, data = bone)

Page 22: (Very) Basic graphing with R

Box plot

Page 23: (Very) Basic graphing with R

## baseboxplot(Wk12lvl ~ Prepar, vitA)

## latticebwplot(Wk12lvl ~ factor(Prepar), vitA)

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

Page 24: (Very) Basic graphing with R

Grouped plot

Page 25: (Very) Basic graphing with R

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

## latticexyplot(Wk12lvl ~ Base1lvl | factor(Prepar), vitA)

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

Page 26: (Very) Basic graphing with R