반응형
/*********************************************************************************************************
-- Title : [R3.3] Visualization - LATTICE 패키지 활용
-- Reference : hrd-net
-- Key word : R 시각화 visualization lattice package 패키지 mlmrev chem97 histogram densityplot dotplot
barchart xyplot cloud 차트 플롯 그래프 graph
*********************************************************************************************************/
-- Chart
-- Python/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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # ******************************************** # -- lattice 패키지 # ******************************************** install.packages("lattice") library(lattice) install.packages("mlmRev") library(mlmRev) data(Chem97) # Chem97 데이터 셋 로드 head(Chem97) str(Chem97) # 차원보기 head(Chem97,30) range(Chem97$score) # 0 10 table(Chem97$score) # 0 2 4 6 8 10 # -- histogram : 변수 대상으로 백분율 적용 히스토그램 생성 # 형식1) (~x축, dataframe) histogram(~gcsescore, data=Chem97) # -- histogram(~x축 | 조건, dataframe) histogram(~gcsescore | score, data=Chem97) histogram(~gcsescore | factor(score), data=Chem97) # -- densityplot : 밀도 그래프 # 형식) (~x축 | 조건, dataframe, groups=변수) densityplot(~gcsescore | factor(score), data=Chem97, groups = gender, plot.points=T, auto.key = T) # ******************************************** # -- 차트 작성을 위한 데이터 리모델링 # ******************************************** # -- 데이터셋 가져오기 data(VADeaths) head(VADeaths) str(VADeaths) # -- 데이터셋 구조보기 mode(VADeaths) # numeric class(VADeaths) # matrix # -- 데이터 리모델링(함수에서 데이터 처리 목적) # matrix -> data.frame 변환 df = as.data.frame(VADeaths) str(df) # 'data.frame': 5 obs. of 4 class(df) df # -- matrix -> data.table 변환 dft = as.data.frame.table(VADeaths) str(dft) # 'data.frame': 20 obs. of 3 variables: class(dft) dft # -- barchart : 막대 그래프 # 형식) (y~x | 조건, dataframe, layout) barchart(Var1 ~ Freq | Var2, data=dft, layout=c(4,1)) # -- dotplot : 점 그래프 # 형식) (y~x | 조건 , dataframe, layout) dotplot(Var1 ~ Freq | Var2 , dft) # -- Var2변수 단위(그룹화)로 점을 연결하여 플로팅 dotplot(Var1 ~ Freq, data=dft, groups=Var2, type="o", auto.key=list(space="right", points=T, lines=T)) # -- xyplot : xyplot()함수 확장 # 형식) (y축~x축| 조건, dataframe or list) library(datasets) str(airquality) # airqulity 테이터 셋 로드 table(airquality$Month) # 5 6 7 8 9 View(airquality) # -- airquality의 Ozone(y),Wind(x) 산점도 플로팅 xyplot(Ozone ~ Wind, data=airquality) # -- Month변수 단위로 플로팅 xyplot(Ozone ~ Wind | Month, data=airquality) xyplot(Ozone ~ Wind | Month, data=airquality, layout=c(5,1)) # -- 지진 발생 데이터셋 head(quakes) # quakes 데이터셋 로드 str(quakes) # -- 지진발생 위치(위도와 경로) xyplot(lat~long, data=quakes, pch=".") # -- 그래프를 변수에 저장 tplot=xyplot(lat~long, data=quakes, pch=".") # 그래프에 제목 추가 tplot2=update(tplot, main="1964년 이후 태평양에서 발생한 지진위치") print(tplot2) # -- equal.count() : 지정된 범위 대상 영역구분과 카운팅 # 형식) equal.count(data, number, overlap) # 비율 척도 -> 범주화 equal.count(1:150, 3, overlap=0) quakes$depth # -- 지진의 깊이를 3영역으로 구분하여 카운팅 depthgroup=equal.count(quakes$depth, number=3, overlap=0) depthgroup # -- depthgroup변수 기준으로 플로팅 xyplot(lat ~ long | depthgroup, data=quakes, main="Fiji Earthquakes(depthgruop)", ylab="latitude", xlab="longitude", pch="@", col='red' ) # -- cloud() : 3차원(위도, 경도, 깊이) 산점도 그래프 cloud(depth ~ lat * long , data=quakes, zlim=rev(range(quakes$depth)), xlab="경도", ylab="위도", zlab="깊이") | cs |
반응형