R로 그래프 그리기

library(UsingR); data(galton)
## Loading required package: MASS
tail(galton)
##     child parent
## 923  73.7   70.5
## 924  73.7   69.5
## 925  73.7   69.5
## 926  73.7   69.5
## 927  73.7   69.5
## 928  73.7   69.5
par(mfrow=c(1,2))
hist(galton$child,col="blue",breaks=100)
hist(galton$parent,col="blue",breaks=100)

plot of chunk galton

부모의 키와 아이의 키를 표료 요약한다

table(galton$child, galton$parent)
##       
##        64 64.5 65.5 66.5 67.5 68.5 69.5 70.5 71.5 72.5 73
##   61.7  1    1    1    0    0    1    0    1    0    0  0
##   62.2  0    1    0    3    3    0    0    0    0    0  0
##   63.2  2    4    9    3    5    7    1    1    0    0  0
##   64.2  4    4    5    5   14   11   16    0    0    0  0
##   65.2  1    1    7    2   15   16    4    1    1    0  0
##   66.2  2    5   11   17   36   25   17    1    3    0  0
##   67.2  2    5   11   17   38   31   27    3    4    0  0
##   68.2  1    0    7   14   28   34   20   12    3    1  0
##   69.2  1    2    7   13   38   48   33   18    5    2  0
##   70.2  0    0    5    4   19   21   25   14   10    1  0
##   71.2  0    0    2    0   11   18   20    7    4    2  0
##   72.2  0    0    1    0    4    4   11    4    9    7  1
##   73.2  0    0    0    0    0    3    4    3    2    2  3
##   73.7  0    0    0    0    0    0    5    3    2    4  0

plot을 하면 겹치는 점이 많아 보기 좋지 않다.

plot(galton)

plot of chunk unnamed-chunk-2

해결방법 1)

겹친 데이타를 피하기 위해 jittering한다.

plot(jitter(child,5) ~ jitter(parent,5),galton)

plot of chunk unnamed-chunk-3

해결방법 2)

sunflowerplot을 그린다.

sunflowerplot(galton)

plot of chunk unnamed-chunk-4

해결방법 3)

점의 모양을 pch=21로 하고 점의 크기(cex)를 조절해서 다음과 같은 그래프를 그린다.

plot of chunk unnamed-chunk-5

Barplot의 예

blue=rbind(c(5,3,4,3),
           c(3,2,5,1))
dimnames(blue)<-list(c("A","B"),c("t1","t2","t3","t4"))
red=rbind(c(1.7,3.5,1.6,1.1),
          c(2.1,1.0,1.7,0.5))
dimnames(red)<-list(c("A","B"),c("t1","t2","t3","t4"))
blue
##   t1 t2 t3 t4
## A  5  3  4  3
## B  3  2  5  1
red
##    t1  t2  t3  t4
## A 1.7 3.5 1.6 1.1
## B 2.1 1.0 1.7 0.5

이것은 탐색적 자료 분석에서 쓸수 있는 간략한 그래프이다.

barplot(blue)

plot of chunk unnamed-chunk-7

barplot(red)

plot of chunk unnamed-chunk-7

이것은 red를 강조해서 튀어나와보이는 그래프이다.

plot of chunk unnamed-chunk-8

data(diamond)
head(diamond)
##   carat price
## 1  0.17   355
## 2  0.16   328
## 3  0.17   350
## 4  0.18   325
## 5  0.25   642
## 6  0.16   342
plot(diamond)

plot of chunk unnamed-chunk-9

plot(diamond,  pch = 21, col = "black", bg = "lightblue", cex = 1.2 )

plot of chunk unnamed-chunk-9

plot(mpg~wt,data=mtcars,pch=21, col="black",bg=cyl*3,cex=1.2)

plot of chunk unnamed-chunk-9