20131004 그래프

Download Report

Transcript 20131004 그래프

2013.10.03
R study
6장, 그래프
1. 산점도
데이터를 점으로 흩뿌리는 형태의 시각화
Plot 함수로 그림
> Methods(plot)
실습




>
>
>
>
install.packages(“mlbench”)
library(mlbench)
Data(Ozone)
Plot(Ozone$V8, Ozone$V9)
축이름 변경




> plot(Ozone$V8, Ozone$V9, xlab=“Sanburg
Temperatuer”,
+ ylab = “E1 Monte Temprature”)
Xlab = x축 이름
Ylab = y축 이름
그래프 제목
 plot
( Ozone $V8 , Ozone $V9 , xlab =" Sandburg
Temperature ",
 + ylab ="El Monte Temperature ", main =" Ozone ")
축 제외하고 제목만 변경할경우
 plot ( Ozone $V8 , Ozone $V9 , main =" Ozone ")
 X, Y
점의 종류 변경




> plot
",
+ ylab
> plot
",
+ ylab
( Ozone $V8 , Ozone $V9 , xlab =" Sandburg Temperature
="El Monte Temperature ", main =" Ozone ", pch =20)
( Ozone $V8 , Ozone $V9 , xlab =" Sandburg Temperature
="El Monte Temperature ", main =" Ozone ", pch="+")

-> “” 로 특수문자포함 문자열 1byte 까지만 가
능한듯

-> pch 코드는 구글에서 ‘r pch symbol’ 키워드
로 검색
점의 크기, 색상변경




> plot ( Ozone $V8 , Ozone $V9 , xlab ="
Sandburg Temperature ",
+ ylab ="El Monte Temperature ", main =" Ozone
", cex=.1)
> plot ( Ozone $V8 , Ozone $V9 , xlab ="
Sandburg Temperature ",
+ ylab ="El Monte Temperature ", main =" Ozone
", col="# FF0000 ")
X, Y 축 값의 범위











> plot ( Ozone $V8 , Ozone $V9 , xlab
Temperature ",
+ ylab ="El Monte Temperature ", main
> max( Ozone $V8)
[1] NA
> max( Ozone $V8 , na.rm = TRUE )
[1] 93
> max( Ozone $V9 , na.rm = TRUE )
[1] 82 .58
> plot ( Ozone $V8 , Ozone $V9 , xlab
Temperature ",
+ ylab ="El Monte Temperature ", main
+ xlim =c(0, 100) , ylim =c(0, 90))
=" Sandburg
=" Ozone ")
=" Sandburg
=" Ozone ",
Type

plot (cars , type ="l")

Type = L

+D
type = o
그래프의 배열








> opar <- par( mfrow =c(1, 2))
> plot ( Ozone $V8 , Ozone $V9 , xlab =" Sandburg
Temperature ",
+ ylab ="El Monte Temperature ", main =" Ozone ")
> plot ( Ozone $V8 , Ozone $V9 , xlab =" Sandburg
Temperature ",
+ ylab ="El Monte Temperature ", main =" Ozone2 ")
> par( opar )
한창에 여러 개의 그래프나열
두개의 그래프를 한번에 비교할때 사용
지터








> plot ( Ozone $V6 , Ozone $V7 , xlab =" Windspeed ",
ylab =" Humidity ",
+ main =" Ozone ", pch =20 , cex=.5)
> plot ( jitter ( Ozone $V6), jitter ( Ozone $V7),
+ xlab =" Windspeed ", ylab =" Humidity ", main ="
Ozone ",
+ pch =20 , cex=.5)
jitter(x, factor = 1, amount = NULL)
-> 값을 조금씩 움직여 점이 겹치는것을 막는다
-> 정확한 데이터를 보여주지못할것같다
지터2
점
> plot ( iris $ Sepal.Width , iris $ Sepal.Length , cex =.5 ,
pch =20 ,
 + xlab =" width ", ylab =" length ", main =" iris ")
 > points ( iris $ Petal.Width , iris $ Petal.Length , cex =.5 ,
 + pch="+", col="# FF0000 ")
이미 생성된 그래프에 점을 추가 색상이나 크기조절도
가능하다

선

> x <- seq (0, 2*pi , 0.1)
> y <- sin(x)
> plot (x, y, cex=.5 , col ="red ")
> lines (x, y)

기존에 그려진 점위에 선을 긋는다



선2


LOWESS는 데이터의 각 점에서 linear model(y = ax + b)
또는 quadratic model(y = ax2 + bx + c) 을 각각 적합하
되, 각 점에서 가까운 데이터에 많은 weight를 주면서
regression을 수행한다.
이렇게 만들어진 결과물은 자료의 추세를 보여주는
선이된다
직선



plot (cars , xlim =c(0, 25) )
abline (a=-5, b=3.5 , col ="red ")
앞서보인 cars 데이터가 dist = −5+3.5×speed로 근사될
수 있다고 가정
곡선


0부터 2π까지의 구간에 대한 sin 곡선
curve (sin , 0, 2*pi)
다각형

선형회귀 거친후 다시 복습
문자열



plot (cars , cex=.5)
text ( cars $speed , cars $dist , pos =4, cex =.5)
데이터 순서에 따라 번호를 붙인다
데이터의 식별



plot (cars , cex=.5)
identify ( cars $speed , cars $ dist )
마우스 클릭으로 해당점의 값을 볼수있다
범례






> plot ( iris $ Sepal.Width , iris $ Sepal.Length , cex =.5 ,
pch =20 ,
+ xlab =" width ", ylab =" length ")
> points ( iris $ Petal.Width , iris $ Petal.Length , cex =.5 ,
+ pch="+", col="# FF0000 ")
> legend (" topright ", legend =c(" Sepal ", " Petal "),
+ pch=c(20 , 43) , cex=.8 , col=c(" black ", "red"), bg="
gray ")
범례2




> x <- seq (-2*pi , 2*pi , 0.01 )
> y <- matrix (c( cos (x), sin (x)), ncol =2)
> matplot (x, y, col=c(" red ", " black "), cex =.2)
> abline (h=0, v =0)
상자그림

boxplot ( iris $ Sepal.Width )

http://techntalk.tistory.com/entry/%EB%B0%95%EC%8A%
A4%ED%94%8C%EB%A1%AF-Box-Plot%EA%B3%BC%EC%A0%95%EA%B7%9C%EB%B6%84%ED%8F%ACnor
mal-distribution%EC%9D%98-%EA%B4%80%EA%B3%84%EB%B0%95%EC%8A%A4%ED%94%8C%EB%A1%AF%EA%B7%B8%EB%A6%AC%EB%8A%94-%EB%B2%95
히스토그램


Hist(Nile)
hist ( iris $ Sepal.Width )
밀도그림


plot ( density ( iris $ Sepal.Width ))
plot(density(Nile))
밀도그림 + 히스토그램


> hist ( iris $ Sepal.Width , freq = FALSE )
> lines ( density ( iris $ Sepal.Width ))
막대 그림


barplot ( tapply ( iris $ Sepal.Width , iris $ Species ,
mean ))
Tapply 는 ‘데이터, 그룹인덱스, 각그룹별로 호출할 함
수를 받는다)
파이그래프









Pie 함수를 이용해 그리며 데이터의 비율을 알아보는
데 적합
구간으로 데이터를 나누기 위해서는 cut() 함수를 사
용한다.
cut (1:10 , breaks =c(0, 5, 10) )
cut (1:10 , breaks =3)
cut( iris $ Sepal.Width , breaks =10)
rep(c("a", "b", "c"), 1:3)
table ( rep(c("a", "b", "c"), 1:3) )
table ( cut( iris $ Sepal.Width , breaks =10) )
pie( table ( cut( iris $ Sepal.Width , breaks =10) ), cex =.7)
모자이크 플롯




str( Titanic )
Titanic
mosaicplot ( Titanic , color = TRUE )
mosaicplot (∼ Class + Survived , data = Titanic , color =
TRUE )
산점도 행렬


pairs (∼ Sepal.Width + Sepal.Length + Petal.Width +
Petal.Length ,
data =iris , col=c("red", " green ", " blue ")[ iris $ Species ])
투시도

X 그리드,Y 그리드, 그리고 각 grid 점에서의 Z 값을 인
자로 받는다.
등고선

contour (x, y, outer (x, y, f))