Skip to content

Instantly share code, notes, and snippets.

@enujo
Created July 28, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enujo/52ed84431d6463912ce5747f6179786f to your computer and use it in GitHub Desktop.
Save enujo/52ed84431d6463912ce5747f6179786f to your computer and use it in GitHub Desktop.
getwd()
# protein.csv 파일을 읽어서 food 데이터 생성
food <- read.csv("./BigData/protein.csv")
# 처음 6개의 관측값을 출력/ 더 많은 관측값을 출력하고 싶으면 head(food, n= 출력하고 싶은 수)
# 마지막 6개의 관측값을 출력하고 싶을 때에는 tail(food) / 자기 자신이 선택하고싶으면 food[1:6,]
head(food)
str(food)
# 붉은 고기와 흰고기를 대상으로 군집
# kmeans(데이터,centers =)
greMeat <- kmeans(food[,c("RedMeat", "WhiteMeat")],centers =3 )
greMeat
# RedMeat 몇퍼+ WhiteMeat 몇퍼가 군집을 이루어 3개의 결과가 나타난다
# 10번 라인의 kmeans 결과가 가독성이 부족함
# 가독성을 고려하여 각 국각가 어느 클러스터에 해당하는지 확인할 수 있도록
result <- order(greMeat$cluster)
data.frame(food$Country[result], greMeat$cluster[result])
# 그래프
# x축은 RedMeat, y축은 WhiteMeat으로 그래프 생성
plot(food$RedMeat,food$WhiteMeat, xlim=c(4,17), xlab="RedMeat", ylab="WhiteMeat",type="n")#x좌표 범위 지정/ type ="n" 점을 사라지게
# 21라인에서 생성한 그래프의 각 위치에 해당하는 국가를 입력
# text(x=x축의 위치, y=y축의 위치, labels=해당하는 위치에 입력될 값을 지정, col=색상의 시작값)
text(x=food$RedMeat, y=food$WhiteMeat, labels = food$Country, col = greMeat$cluster)
# 모든 섭취원에 대해 5개의 클러스터로 군집화
greMeat.All <- kmeans(food[,-1],centers =5 )
greMeat.All
result.All <- order(greMeat$cluster)
data.frame(food$Country[result.All], greMeat$cluster[result.All])
plot(food$RedMeat,food$WhiteMeat, xlim=c(2,20), xlab="RedMeat", ylab="WhiteMeat",type="n")#x좌표 범위 지정/ type ="n" 점을 사라지게
text(x=food$RedMeat, y=food$WhiteMeat, labels = food$Country, col = rainbow(7))
plot(food$Milk,food$Eggs,xlim=c(0,30),xlab="Milk", ylab="Eggs", type="n")
text(x=food$Milk, y=food$Eggs, labels=food$Country,col=greMeat.All$cluster)
# 모든 섭취원에 대한 5개의 군집(greMeat.All)에서 Cereals과 Fish의 섭취량을 기준으로 그래프를 생성
# x축 y축 셋팅을 안하면 알아서 자동으로 표준화 시켜준다.
plot(food$Cereals, food$Fish, xlab="Cereals", ylab="Fish", type="n")
text(x=food$Cereals, y=food$Fish, labels=food$Country,col=greMeat.All$cluster)
# 지도위에 나라이름 해당하는 나라의 위도와 경도가 있다. 그 위치에 이름을 쓰는게 아니라 점을 찍어주면 된다.
# 그러기 위해서는 군집의 결과를 패키지로 지도패키지 설치/로드
install.packages("ggmap")
library(ggmap)
install.packages("rworldmap")
library(rworldmap)
# 지도에 그래프를 그리기 위해서는 필요한 것이 있다.
# 1. 나라 이름/ 2. 국가 대표 좌표(위도/경도)
# R패키지에서 사용하는 ggmap과rworldmap은 character형식으로 접근해야한다.
# food에 포함되어있는 국가명을 추출하여 chararcter 형식으로 데이터를 생성 -> country
country <- as.character(food$Country)
country
# 추출한 국가의 위도와 경도 값을 가져와야 함, 지도의 위치값으로 변환 : geocode() 자동으로 국가명을 가져와 위도와 경도를 위치값으로 변환(location)
eu.limits <- geocode(country)
eu.limits
# 지도를 그리기 위해서 getMap함수를 사용 해상도가 높은 것은 에러 백퍼 발생 !
newMap <- getMap(resolution = "low") # low 흑백으로 가져옴
# 지도가 2행 1열로 출력 아래 위 오른쪽 왼
par(mfrow=c(2,1), mai=c(0.3,0.6,0.1,0.1))
#그리기 x좌표의 길이는
plot(newMap, xlim=range(eu.limits$lon), ylim=range(eu.limits$lat), asp=1)
points(eu.limits$lon, eu.limits$lat, col=greMeat$cluster, pch=20, cex=2)
text(eu.limits$lon, eu.limits$lat, labels=food$Country, cex=1.1)
# 그래프 생성하니 위치정보가 잘못된 국가가 있다 -> 위치 정보를 쉉
# Czeochos 5, USSR 23 항상 값은 벡터형식으로
eu.limits[5,] <- c(14.26, 50.50)
eu.limits[23,] <- c(27.3929020, 55.7215090)
plot(newMap, xlim=range(eu.limits$lon), ylim=range(eu.limits$lat), asp=1)
points(eu.limits$lon, eu.limits$lat, col=greMeat$cluster, pch=20, cex=2)
# lat을 빼면 글씨가 1만큼 위도가 이동한 상태에서 text가 출력된다.
# text(eu.limits$lon,eu.limits$lat,labels=food$Country, cex=1.1)
text(eu.limits$lon,eu.limits$lat-1,labels=food$Country, cex=1)
# greMeat 클러스터와 비교하기 위해 greMeat.All 클러스터와 대상으로하는 지도를 출력
plot(newMap, xlim=range(eu.limits$lon), ylim=range(eu.limits$lat), asp=1)
points(eu.limits$lon, eu.limits$lat, col=greMeat.All$cluster, pch=20, cex=2)
text(eu.limits$lon,eu.limits$lat-1,labels=food$Country, cex=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment