2013.10.03 R study

33
2013.10.03 R study 6 장 , 장장장

description

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 ”, - PowerPoint PPT Presentation

Transcript of 2013.10.03 R study

Page 1: 2013.10.03  R study

2013.10.03 R study

6 장 , 그래프

Page 2: 2013.10.03  R study

1. 산점도

데이터를 점으로 흩뿌리는 형태의 시각화

Plot 함수로 그림

> Methods(plot)

Page 3: 2013.10.03  R study

실습 > install.packages(“mlbench”) > library(mlbench) > Data(Ozone) > Plot(Ozone$V8, Ozone$V9)

Page 4: 2013.10.03  R study

축이름 변경 > plot(Ozone$V8, Ozone$V9, xlab=“Sanburg Temperatuer”,

+ ylab = “E1 Monte Temprature”)

Xlab = x 축 이름 Ylab = y 축 이름

Page 5: 2013.10.03  R study

그래프 제목 plot ( Ozone $V8 , Ozone $V9 , xlab =" Sand-

burg Temperature ", + ylab ="El Monte Temperature ", main ="

Ozone ")

X, Y 축 제외하고 제목만 변경할경우 plot ( Ozone $V8 , Ozone $V9 , main =" Ozone

")

Page 6: 2013.10.03  R study

점의 종류 변경 > plot ( Ozone $V8 , Ozone $V9 , xlab =" Sandburg Temperature ",

+ ylab ="El Monte Temperature ", main =" Ozone ", pch =20)

> plot ( Ozone $V8 , Ozone $V9 , xlab =" Sandburg Temperature ",

+ ylab ="El Monte Temperature ", main =" Ozone ", pch="+")

-> “” 로 특수문자포함 문자열 1byte 까지만 가능한듯

-> pch 코드는 구글에서 ‘ r pch symbol’ 키워드로 검색

Page 7: 2013.10.03  R study

점의 크기 , 색상변경 > 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 ")

Page 8: 2013.10.03  R study

X, Y 축 값의 범위 > plot ( Ozone $V8 , Ozone $V9 , xlab =" Sandburg Temperature ",

+ ylab ="El Monte Temperature ", main =" Ozone ") > 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 =" Sandburg Temperature ",

+ ylab ="El Monte Temperature ", main =" Ozone ", + xlim =c(0, 100) , ylim =c(0, 90))

Page 9: 2013.10.03  R study

Type plot (cars , type ="l")

Type = L type = o

+ D

Page 10: 2013.10.03  R study

그래프의 배열 > 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 )

한창에 여러 개의 그래프나열 두개의 그래프를 한번에 비교할때 사용

Page 11: 2013.10.03  R study

지터 > plot ( Ozone $V6 , Ozone $V7 , xlab =" Wind-

speed ", 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) -> 값을 조금씩 움직여 점이 겹치는것을 막는다 -> 정확한 데이터를 보여주지못할것같다

Page 12: 2013.10.03  R study

지터 2

Page 13: 2013.10.03  R study

점 > 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 ")이미 생성된 그래프에 점을 추가 색상이나 크기조절도가능하다

Page 14: 2013.10.03  R study

선 > x <- seq (0, 2*pi , 0.1) > y <- sin(x) > plot (x, y, cex=.5 , col ="red ") > lines (x, y)

기존에 그려진 점위에 선을 긋는다

Page 15: 2013.10.03  R study

선 2 LOWESS 는 데이터의 각 점에서 linear model(y =

ax + b) 또는 quadratic model(y = ax2 + bx + c) 을 각각 적합하되 , 각 점에서 가까운 데이터에 많은 weight 를 주면서 regression 을 수행한다 .

이렇게 만들어진 결과물은 자료의 추세를 보여주는 선이된다

Page 16: 2013.10.03  R study

직선 plot (cars , xlim =c(0, 25) ) abline (a=-5, b=3.5 , col ="red ")

앞서보인 cars 데이터가 dist = −5+3.5×speed 로 근사될 수 있다고 가정

Page 17: 2013.10.03  R study

곡선 0 부터 2π 까지의 구간에 대한 sin 곡선 curve (sin , 0, 2*pi)

Page 18: 2013.10.03  R study

다각형 선형회귀 거친후 다시 복습

Page 19: 2013.10.03  R study

문자열 plot (cars , cex=.5) text ( cars $speed , cars $dist , pos =4, cex

=.5) 데이터 순서에 따라 번호를 붙인다

Page 20: 2013.10.03  R study

데이터의 식별 plot (cars , cex=.5) identify ( cars $speed , cars $ dist ) 마우스 클릭으로 해당점의 값을 볼수있다

Page 21: 2013.10.03  R study

범례 > 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 ")

Page 22: 2013.10.03  R study

범례 2

Page 23: 2013.10.03  R study

> 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)

Page 24: 2013.10.03  R study

상자그림 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%ACnormal-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

Page 25: 2013.10.03  R study

히스토그램 Hist(Nile) hist ( iris $ Sepal.Width )

Page 26: 2013.10.03  R study

밀도그림 plot ( density ( iris $ Sepal.Width )) plot(density(Nile))

Page 27: 2013.10.03  R study

밀도그림 + 히스토그램 > hist ( iris $ Sepal.Width , freq = FALSE ) > lines ( density ( iris $ Sepal.Width ))

Page 28: 2013.10.03  R study

막대 그림 barplot ( tapply ( iris $ Sepal.Width , iris $

Species , mean ))

Tapply 는 ‘데이터 , 그룹인덱스 , 각그룹별로 호출할 함수를 받는다 )

Page 29: 2013.10.03  R study

파이그래프 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)

Page 30: 2013.10.03  R study

모자이크 플롯 str( Titanic ) Titanic mosaicplot ( Titanic , color = TRUE )

mosaicplot (∼ Class + Survived , data = Ti-tanic , color = TRUE )

Page 31: 2013.10.03  R study

산점도 행렬 pairs (∼ Sepal.Width + Sepal.Length +

Petal.Width + Petal.Length , data =iris , col=c("red", " green ", " blue ")

[ iris $ Species ])

Page 32: 2013.10.03  R study

투시도 X 그리드 , Y 그리드 , 그리고 각 grid 점에서의 Z 값을 인자로 받는다 .

Page 33: 2013.10.03  R study

등고선 contour (x, y, outer (x, y, f))