반응형
/*********************************************************************************************************
-- Title : [R3.3] Visualization - plot() 적용, 중복 데이터, 산점도 Matrix 시각화
-- Reference : hrd-net
-- Key word : R 시각화 visualization plot() pairs 산점도 galton
*********************************************************************************************************/
-- Chart
-- R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | # ******************************************** # -- plot() 일반 객체 적용 # ******************************************** methods(plot) # 메쏘드 개수 : 34 head(galton) par(mfrow=c(1,1)) # -- lm함수를 이용하여 회귀분석 # lm(종속변수 ~ 설명변수, 모델데이터) model =lm(child ~ parent, data=galton) model plot(model) # Console 창에서 <Enter>를 눌러야.. # -- 산점도와 회귀선 시각화(galton 데이터 셋) par(mfrow=c(1,1)) plot(child~parent, data=galton) out = lm(child~parent, data=galton) abline(out, col="red") # ******************************************** # -- 중복 데이터 시각화 # ******************************************** x = c(1:4,2,4) # 1 2 3 4 2 4 y = rep(2, 6) # 2 2 2 2 2 2 x; y plot(x, y) # 교차테이블 # 교차 항목의 개 table(x, y) # y # x 2 # 1 1 # 2 2 # 3 1 # 4 2 xy_df = data.frame(table(x, y)) xy_df # x y Freq plot(y ~ x, cex=0.8*xy_df$Freq, pch='@', col='red') # -- 데이터프레임으로 변환 library(psych) freqData = as.data.frame(table(galton$child, galton$parent)) names(freqData)=c("child","parent", "freq") # 컬럼명 지정 # (2) 프레임 -> 벡터 -> 수치데이터변환 -> 가중치 적용 parent = as.numeric(as.vector(freqData$parent)) child = as.numeric(as.vector(freqData$child)) plot(child~parent, pch=21, col="blue", bg="green", cex=0.15*freqData$freq, xlab="parent", ylab="child") plot(x, y) plot(y ~ x) # ******************************************** # -- 변수 산점도 matrix # ******************************************** data(iris) head(iris) # -- 4개 변수 상호비교 pairs(iris[,1:4]) # Sepal.Length Sepal.Width Petal.Length Petal.Width # -- Species=="virginica"인 경우만 4개 변수 상호비교 iris[iris$Species=="virginica", 1:4] pairs(iris[iris$Species=="virginica", 1:4]) pairs(iris[iris$Species=="setosa", 1:4]) # ******************************************** # -- 차트 결과 파일 저장 # ******************************************** setwd("C:/Rwork/Part-II") # 폴더 지정 jpeg("galton.jpg", width=720, height=480) # open-픽셀 지정 가능 plot(child~parent, data=galton) # y ~ x title(main="부모와 자식의 키 유전관계") out = lm(child~parent, data=galton) abline(out, col="red") dev.off() # close - 장치 종료 | cs |
반응형