59doit

[ R ]다양한 시각화 # 3 데이터범주화 본문

Programming/R

[ R ]다양한 시각화 # 3 데이터범주화

yul_S2 2022. 12. 23. 14:01
반응형

(6)  데이터 범주화 

데이터를 일정구간으로 범주화 작업

lattice 패키지의 equal.count()함수 이용

 equal.count(data, number=n, overlap =0)

 

# equal.count()함수를 사용하여 이산형 변수 범주화하기

#1 1~150 대상으로 겹치지 않게 4개 영역으로 범주화

numgroup <- equal.count(1:150,number = 4overlap = 0)
numgroup

 

 

#2 지진의 깊이를 5개 영역으로 범주화

depthgroup <- equal.count(quakes$depth, number = 5, overlap = 0)
depthgroup

 

 

#3 범주화된 변수(depthgroup)를 조건으로 산점도 그리기

xyplot(lat~long | depthgroup, data=quakes,
       main = "Earthquakes(depthgroup)",
       ylab="latitude", xlab="longitude",
       pch=".",col="red")

main: 차트 제목

ylab: y축 이름

xlab: x축 이름

pch=” ”: 점 표현문자

col=’ ’: 점 색상

 

 

 

# 수심과 리히터 규모 변수를 동시에 적용하여 산점도 그리기

#1 리히터 규모를 2개 영역으로 구분

magnitudegroup <- equal.count(quakes$mag, number = 2, overlap = 0)
magnitudegroup

 

 

#2 magnitudegroup 변수를 기준으로 산점도 그리기

xyplot(lat~long|magnitudegroup, data=quakes,
       main = "Earthquakes(magnitude)",
       ylab="latitude", xlab = "longitude",
       pch = "*", col = "blue")



 

#2 수심(depthgroup)과 리히터 규모(magnitudegroup) 동시에 표현

xyplot(lat~long|depthgroup*magnitudegroup, data=quakes,
       main = "Earthquakes",
       ylab="latitude", xlab="longitude",
       pch="o", col=c("red","blue"))

동시에 표현되기 때문에 이를 구분하기 위해 2개의 색상으로 구분

수심(depth)변수와 mag(리히터 규모)변수는 연속형 변수이기 때문에 이들을 이산형 변수로 변경한후 factor형으로 변환하여 산점도를 그린다.

 

 

# 이산형 변수를 리코딩한 뒤에 factor형으로 변환하여 산점도 그리기

#1 depth변수 리코딩

quakes$depth3[quakes$depth >= 39.5 & quakes$depth <= 80.5] <- 'd1'
quakes$depth3[quakes$depth >= 79.5 & quakes$depth <= 186.5] <- 'd2'
quakes$depth3[quakes$depth >= 185.5 & quakes$depth <= 397.5] <- 'd3'
quakes$depth3[quakes$depth >= 396.5 & quakes$depth <= 562.5] <- 'd4'
quakes$depth3[quakes$depth >= 562.5 & quakes$depth <= 680.5] <- 'd5'

 

 

#2 mag변수 리코딩

quakes$mag3[quakes$mag >= 3.95 & quakes$mag <= 4.65] <- 'm1'
quakes$mag3[quakes$mag >= 4.55 & quakes$mag <= 6.65] <- 'm2'

 

 

#3 factor 형 변환

convert <- transform(quakes,depth3=factor(depth3),mag3=factor(mag3))

 

 

#4 산점도 그래프 그리기

xyplot(lat~long|depth3*mag3, data = convert,
       main = "Earthquakes",
       ylab= "latitude", xlab="longitude",
       pch = "o",col = c("red","blue"))


 

 

(6)  데이터 범주화 

데이터를 일정구간으로 범주화 작업

lattice 패키지의 equal.count()함수 이용

 equal.count(data, number=n, overlap =0)

 

# equal.count()함수를 사용하여 이산형 변수 범주화하기

#1 1~150 대상으로 겹치지 않게 4개 영역으로 범주화

numgroup <- equal.count(1:150,number = 4overlap = 0)
numgroup

 

 

#2 지진의 깊이를 5개 영역으로 범주화

depthgroup <- equal.count(quakes$depth, number = 5, overlap = 0)
depthgroup

 

 

#3 범주화된 변수(depthgroup)를 조건으로 산점도 그리기

xyplot(lat~long | depthgroup, data=quakes,
       main = "Earthquakes(depthgroup)",
       ylab="latitude", xlab="longitude",
       pch=".",col="red")

main: 차트 제목

ylab: y축 이름

xlab: x축 이름

pch=” ”: 점 표현문자

col=’ ’: 점 색상

 

 

 

# 수심과 리히터 규모 변수를 동시에 적용하여 산점도 그리기

#1 리히터 규모를 2개 영역으로 구분

magnitudegroup <- equal.count(quakes$mag, number = 2, overlap = 0)
magnitudegroup

 

 

#2 magnitudegroup 변수를 기준으로 산점도 그리기

xyplot(lat~long|magnitudegroup, data=quakes,
       main = "Earthquakes(magnitude)",
       ylab="latitude", xlab = "longitude",
       pch = "*", col = "blue")



 

#2 수심(depthgroup)과 리히터 규모(magnitudegroup) 동시에 표현

xyplot(lat~long|depthgroup*magnitudegroup, data=quakes,
       main = "Earthquakes",
       ylab="latitude", xlab="longitude",
       pch="o", col=c("red","blue"))

동시에 표현되기 때문에 이를 구분하기 위해 2개의 색상으로 구분

수심(depth)변수와 mag(리히터 규모)변수는 연속형 변수이기 때문에 이들을 이산형 변수로 변경한후 factor형으로 변환하여 산점도를 그린다.

 

 

# 이산형 변수를 리코딩한 뒤에 factor형으로 변환하여 산점도 그리기

#1 depth변수 리코딩

quakes$depth3[quakes$depth >= 39.5 & quakes$depth <= 80.5] <- 'd1'
quakes$depth3[quakes$depth >= 79.5 & quakes$depth <= 186.5] <- 'd2'
quakes$depth3[quakes$depth >= 185.5 & quakes$depth <= 397.5] <- 'd3'
quakes$depth3[quakes$depth >= 396.5 & quakes$depth <= 562.5] <- 'd4'
quakes$depth3[quakes$depth >= 562.5 & quakes$depth <= 680.5] <- 'd5'

 

 

#2 mag변수 리코딩

quakes$mag3[quakes$mag >= 3.95 & quakes$mag <= 4.65] <- 'm1'
quakes$mag3[quakes$mag >= 4.55 & quakes$mag <= 6.65] <- 'm2'

 

 

#3 factor 형 변환

convert <- transform(quakes,depth3=factor(depth3),mag3=factor(mag3))

 

 

#4 산점도 그래프 그리기

xyplot(lat~long|depth3*mag3, data = convert,
       main = "Earthquakes",
       ylab= "latitude", xlab="longitude",
       pch = "o",col = c("red","blue"))


 

 

반응형
Comments