2006 10 16 http://phi.med.gunma-u.ac.jp/medstat/p01.xls p01.txt R p01 <- read.delim("p01.txt") str(p01) 100 pid int sex F M 2 Factor ht wt num data.frame : 100 obs. of 4 variables: $ pid: int 1 2 3 4 5 6 7 8 9 10... $ sex: Factor w/ 2 levels "F","M": 1 2 1 1 2 1 2 2 1 1... $ ht : num 165 169 160 163 172... $ wt : num 61.3 65.7 57.6 62.9 58.3 55.2 70.2 60.6 60.3 58.9... summary(p01) NA pid sex 100 ht 99 1 wt 98 2 pid sex ht wt Min. : 1.00 F:50 Min. :150.6 Min. :45.60 1st Qu.: 25.75 M:50 1st Qu.:160.2 1st Qu.:58.08 Median : 50.50 Median :165.0 Median :61.95 Mean : 50.50 Mean :165.1 Mean :61.93 3rd Qu.: 75.25 3rd Qu.:169.7 3rd Qu.:66.40 Max. :100.00 Max. :181.3 Max. :76.60 NA s : 1.0 NA s : 2.00 p01s <- subset(p01,complete.cases(p01)) p01s str(p01s) summary(p01s) 97 1
R int num factor character 1 2 (dichotomous variable) (trichotomous variable) 3 (nominal scale) M F 1 2 coding 1 2 3 4 4 1 as.numeric() as.integer() 2 as.ordered() 3 4 2
(ordinal scale) (Ord.factor) R as.ordered() +++ ++ + ± + 3, 2, 1, 0.5, 0 3 2 2 1 3 2 2 1 3, 2, 1, 0.5, 0 as.ordered(o.blood) 3, 2, 1 15, 3.14159265358979, 1 1 5, 4, 3, 2, 1 (interval scale) 5 39 36 39/36 1.083 6 5 6 cm m 3
(ratio scale) 7 cm kg 8 TFR 9 10 100cm cm 170cm 2cm 7 8 9 PowerPoint OpenOffice.org Impress PowerPoint 10 OpenOffice.org Draw Adobe Illustrator 4
(2005) (ISBN4-06-149773-1) R pdf jpg png tiff Windows Macintosh Linux postscript 11 R source() 12 X R barplot(table(x)) M + + + 4 ++ 1 + 2 ± 12 97 table(x) c() names() barplot() OpenOffice.org Draw 11 table() 12 source("http://phi.med.gunma-u.ac.jp/medstat/it02-3.r") R Console URL R Editor 5
it02-3.r ob <- c(4,1,2,12,97) names(ob) <- c("+++","++","+"," ","-") barplot(ob,ylim=c(0,100),main=" \n ") 2 barplot() it02-4.r ob <- c(4,1,2,12,97) names(ob) <- c("+++","++","+"," ","-") ii <- barplot(matrix(ob,nrow(ob)),beside=f,ylim=c(0,120),main=" ") oc <- ob for (i in 1:length(ob)) { oc[i] <- sum(ob[1:i])-ob[i]/2 } text(ii,oc,paste(names(ob))) + + + ++ 0 + 1 ± 5 47 + + + 4 ++ 1 + 1 ± 7 50 R 6
it02-5.r obm <- c(0,0,1,5,47) obf <- c(4,1,1,7,50) obx <- cbind(obm,obf) rownames(obx) <- c("+++","++","+"," ","-") colnames(obx) <- c(" "," ") ii <- barplot(obx,beside=f,ylim=c(0,70),main=" ") oc <- obx for (i in 1:length(obx[,1])) { oc[i,1] <- sum(obx[1:i,1])-obx[i,1]/2 } for (i in 1:length(obx[,2])) { oc[i,2] <- sum(obx[1:i,2])-obx[i,2]/2 } text(ii[1],oc[,1],paste(rownames(obx))) text(ii[2],oc[,2],paste(rownames(obx))) 100% R 2 (%) 4 horiz=t it02-6.r ob <- c(4,1,2,12,97) obp <- ob/sum(ob)*100 names(obp) <- c("+++","++","+"," ","-") ii <- barplot(matrix(obp,nrow(obp)),horiz=t,beside=f,xlim=c(0,100), xlab="(%)",main=" ") oc <- obp for (i in 1:length(obp)) { oc[i] <- sum(obp[1:i])-obp[i]/2 } text(oc,ii,paste(names(obp))) 7
barplot() dotchart() it02-7.r obm <- c(0,0,1,5,47) obf <- c(4,1,1,7,50) obx <- cbind(obm,obf) rownames(obx) <- c("+++","++","+"," ","-") colnames(obx) <- c(" "," ") dotchart(obx) dotchart(t(obx)) 100% 8
13 R pie() 14 R it02-8.r ob <- c(4,1,2,12,97) names(ob) <- c("+++","++","+"," ","-") pie(ob) Excel R hist() 15 90 10 1 16 it02-9.r dat <- read.delim("http://phi.med.gunma-u.ac.jp/medstat/p01.txt") hist(dat$ht,main=" ") 13 Cleveland Cleveland WS (1985) The elements of graphing data. Wadsworth, Monterey, CA, USA. p.264 R help 14 R-1.5 piechart() 15 http://phi.med.gunma-u.ac.jp/demography/makepyramid.html 16 http://phi.med.gunma-u.ac.jp/medstat/p01.txt 9
qqnorm() qqline() it02-10.r dat <- read.delim("http://phi.med.gunma-u.ac.jp/medstat/p01.txt") qqnorm(dat$ht,main=" ",ylab=" (cm)") qqline(dat$ht,lty=2) (stem and leaf plot) 5 10 R stem() 90 17 dat <- read.delim("http://phi.med.gunma-u.ac.jp/medstat/p01.txt") stem(dat$ht) (box and whisker plot) (median) 1/4 (first quartile) 1/4 (third quartile) 1.5 17 source("http://phi.med.gunma-u.ac.jp/swtips/gstem.r") stem() gstem() 10
R boxplot() dat <- read.delim("http://phi.med.gunma-u.ac.jp/medstat/p01.txt") boxplot(dat$ht) (stripchart) R stripchart() vert=t it02-11.r dat <- read.delim("http://phi.med.gunma-u.ac.jp/medstat/p01.txt") attach(dat) mht <- tapply(ht,sex,mean) sht <- tapply(ht,sex,sd) IS <- c(1,2)+0.15 stripchart(ht~sex,method="jitter",vert=t,ylab=" (cm)") points(is,mht,pch=18) arrows(is,mht-sht,is,mht+sht,code=3,angle=90,length=.1) detach(dat) (scatter plot) R plot() plot() pch points() symbols() 18 18 http://phi.med.gunma-u.ac.jp/medstat/semen.r 11
matplot() matpoints() pairs() text() identify() dat <- read.delim("http://phi.med.gunma-u.ac.jp/medstat/p01.txt") plot(dat$ht,dat$wt,pch=paste(dat$sex),xlab=" (cm)",ylab=" (kg)") R stars() maptools ESRI GIS 19 http://phi.med.gunma-u.ac.jp/medstat/p02.txt 1 Excel (wt) R A4 1 19 http://phi.med.gunma-u.ac.jp/swtips/epimap.html 12