Vorsprung durch Technik

블로그 이미지

MSNU

R 언어로 그래프 그리기

myPPT 2015. 12. 26. 20:59











































R  

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



 X, Y 축 제외하고 제목만 변경할경우

 plot ( Ozone $V8 , Ozone $V9 , main =" Ozone ")


점의 종류 변경

 > 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’ 키워드로 검색

점의 크기, 색상변경

 > 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 =" 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))

Type

 plot (cars , type ="l")


 Type = L                           type = o








 + D

그래프의 배열

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



히스토그램

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





'myPPT' 카테고리의 다른 글

C언어 - 포인터Pointer  (1) 2016.01.10
3D 입체영상 기술  (0) 2015.12.29
Digital Convergence(디지털 컨버전스)  (0) 2015.12.22
낭만주의 (Romantic Era)  (0) 2015.12.18
음운의 변동  (0) 2015.12.14
Posted by MSNU






favicon

Vorsprung durch Technik

  • 태그
  • 링크 추가
  • 방명록

관리자 메뉴

  • 관리자 모드
  • 글쓰기
  • 분류 전체보기 (993)
    • myPPT (813)
    • 시리즈 (164)
      • 연소 (14)
      • 경제 (5)

카테고리

PC화면 보기 티스토리 Daum

티스토리툴바