IT기술/BigData

Case 8. ggplot2() 와 ggmap 을 사용한 지도위에 표기하기

dobbby 2014. 2. 24. 17:55
반응형

아래의 예는 ggplot2() 패키지와 ggmap 을 활용하는 방법을 보여주고 있으며 원문은

http://freesearch.pe.kr/archives/3953 입니다.


> rm(list=ls())

> install.packages("doParallel")

Installing package into ‘C:/Users/stu/Documents/R/win-library/3.0’

(as ‘lib’ is unspecified)

trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/doParallel_1.0.7.zip'

Content type 'application/zip' length 184085 bytes (179 Kb)

opened URL

downloaded 179 Kb


package ‘doParallel’ successfully unpacked and MD5 sums checked


The downloaded binary packages are in

        C:\Users\stu\AppData\Local\Temp\RtmpMB6sAR\downloaded_packages

> library(doParallel)

Loading required package: foreach

foreach: simple, scalable parallel programming from Revolution Analytics

Use Revolution R for scalability, fault tolerance and more.

http://www.revolutionanalytics.com

Loading required package: iterators

Loading required package: parallel


> registerDoParallel(cores=4)    #최대 CPU 사용개수


# naver map 과 연결

> eq <- read.table(url("http://dl.dropbox.com/u/8686172/eq.csv", encoding = "euc-kr"), sep = "\t", header = T, stringsAsFactors = F)

> eq$latitude <- unlist(strsplit(eq$latitude, " "))[seq(from = 1, to = nrow(eq), by = 2)]

> eq$longitude <- unlist(strsplit(eq$longitude, " "))[seq(from = 1, to = nrow(eq), by = 2)]

> install.packages("lubridate")

Installing package into ‘C:/Users/stu/Documents/R/win-library/3.0’

(as ‘lib’ is unspecified)

also installing the dependency ‘memoise’


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/memoise_0.1.zip'

Content type 'application/zip' length 10921 bytes (10 Kb)

opened URL

downloaded 10 Kb


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/lubridate_1.3.3.zip'

Content type 'application/zip' length 662873 bytes (647 Kb)

opened URL

downloaded 647 Kb


package ‘memoise’ successfully unpacked and MD5 sums checked

package ‘lubridate’ successfully unpacked and MD5 sums checked


The downloaded binary packages are in

        C:\Users\stu\AppData\Local\Temp\RtmpMB6sAR\downloaded_packages

> library(lubridate)

> eq$longitude <- as.double(eq$longitude)

> eq$latitude <- as.double(eq$latitude)

> eq$year <- as.factor(substr(eq$date, 1, 4))

> eq$date <- ymd_hm(eq$date)

> cent <- c(126.96136, 37.52962)

> install.packages("devtools")

Installing package into ‘C:/Users/stu/Documents/R/win-library/3.0’

(as ‘lib’ is unspecified)

also installing the dependencies ‘bitops’, ‘httr’, ‘RCurl’, ‘whisker’, ‘evaluate’


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/bitops_1.0-6.zip'

Content type 'application/zip' length 35878 bytes (35 Kb)

opened URL

downloaded 35 Kb


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/httr_0.2.zip'

Content type 'application/zip' length 137331 bytes (134 Kb)

opened URL

downloaded 134 Kb


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/RCurl_1.95-4.1.zip'

Content type 'application/zip' length 2836490 bytes (2.7 Mb)

opened URL

downloaded 2.7 Mb


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/whisker_0.3-2.zip'

Content type 'application/zip' length 64620 bytes (63 Kb)

opened URL

downloaded 63 Kb


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/evaluate_0.5.1.zip'

Content type 'application/zip' length 48276 bytes (47 Kb)

opened URL

downloaded 47 Kb


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/devtools_1.4.1.zip'

Content type 'application/zip' length 240656 bytes (235 Kb)

opened URL

downloaded 235 Kb


package ‘bitops’ successfully unpacked and MD5 sums checked

package ‘httr’ successfully unpacked and MD5 sums checked

package ‘RCurl’ successfully unpacked and MD5 sums checked

package ‘whisker’ successfully unpacked and MD5 sums checked

package ‘evaluate’ successfully unpacked and MD5 sums checked

package ‘devtools’ successfully unpacked and MD5 sums checked


The downloaded binary packages are in

        C:\Users\stu\AppData\Local\Temp\RtmpMB6sAR\downloaded_packages

> library(devtools)

WARNING: Rtools is required to build R packages, but is not currently installed.


Please download and install Rtools 3.1 from http://cran.r-project.org/bin/windows/Rtools/ and then run find_rtools().

> install_github("ggmap", "haven-jeon")

Installing github repo ggmap/master from haven-jeon

Downloading ggmap.zip from https://github.com/haven-jeon/ggmap/archive/master.zip

Installing package from C:\Users\stu\AppData\Local\Temp\RtmpMB6sAR/ggmap.zip

Installing ggmap

Installing dependencies for ggmap:

geosphere

Installing package into ‘C:/Users/stu/Documents/R/win-library/3.0’

(as ‘lib’ is unspecified)

also installing the dependency ‘sp’


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/sp_1.0-14.zip'

Content type 'application/zip' length 1482770 bytes (1.4 Mb)

opened URL

downloaded 1.4 Mb


trying URL 'http://cran.nexr.com/bin/windows/contrib/3.0/geosphere_1.3-8.zip'

Content type 'application/zip' length 992218 bytes (968 Kb)

opened URL

downloaded 968 Kb


package ‘sp’ successfully unpacked and MD5 sums checked

package ‘geosphere’ successfully unpacked and MD5 sums checked


The downloaded binary packages are in

        C:\Users\stu\AppData\Local\Temp\RtmpMB6sAR\downloaded_packages

"C:/PROGRA~1/R/R-30~1.2/bin/i386/R" --vanilla CMD INSTALL  \

  "C:\Users\stu\AppData\Local\Temp\RtmpMB6sAR\devtools15883f14395d\ggmap-master"  \

  --library="C:/Users/stu/Documents/R/win-library/3.0" --install-tests 


* installing *source* package 'ggmap' ...

** R

** data

*** moving datasets to lazyload DB

** inst

** preparing package for lazy loading

** help

*** installing help indices

** building package indices

** testing if installed package can be loaded

* DONE (ggmap)

> library(ggmap)

Google Maps API Terms of Service : http://developers.google.com/maps/terms

> bmap <- ggmap(get_navermap(center = cent, level = 4, baselayer = "default", overlayers = c("anno_satellite", "traffic"), marker = data.frame(cent[1], cent[2]), key = "c75a09166a38196955adee04d3a51bf8", uri = "www.r-project.org"), extent = "device", base_layer = ggplot(eq, aes(x = longitude, y = latitude)))

Using archived map...

> bmap + geom_point(aes(size = power, colour = date), data = eq, alpha = 0.7) + geom_density2d()

Warning messages:

1: Removed 936 rows containing non-finite values (stat_density2d). 

2: Removed 936 rows containing missing values (geom_point). 





반응형