2014年9月21日星期日

draw map with R---something easy and amazing

How to visualize data on Maps by R?

Hi, everyone! I came back! After several days of study I found something that is super interesting in R: drawing maps.

There three ways of drawing maps:1. get you own map data. 2.get data from r maps and map data library. 3 use ggmap library.


Using your own data:
library(maps)
library(mapdata)
#install.packages("maptools")
library(maptools)
#install.packages("sp")
library(sp)
x=readShapeSpatial("/Users/leilei/Desktop/The Last Semester/ADA/china-province-border-data/bou2_4p.shp")
install.packages("gpclib")
gpclibPermit()
gpclibPermitStatus()
plot(x)

Using map library:
library(maps)
map("world", fill = TRUE, col = rainbow(200),
    ylim = c(-60, 90), mar = c(0, 0, 0, 0))
title("world map")
What a colorful map!~

Using GGmap:
library(ggmap)
library(ggplot2)
geocode("Fordham University", output = "more")

output:
       lon      lat       type     loctype                                  address
1 -73.8857 40.86204 university approximate fordham university, bronx, ny 10458, usa
     north    south      east      west postal_code       country
1 40.86469 40.85781 -73.88066 -73.89047       10458 united states
  administrative_area_level_2 administrative_area_level_1 locality street streetNo
1                bronx county                    new york     <NA>   <NA>       NA
  point_of_interest              query
1              <NA> Fordham University

Also, you can draw gif by ggmap:

library(ggmap)
library(animation)
library(XML)
library(ggplot2)
webpage <-'http://www.ldeo.columbia.edu/cgi-bin/quake.cgi'
#download earthquake data from this website
tables <- readHTMLTable(webpage,stringsAsFactors = FALSE)
raw <- tables[[1]]
data <- raw[-1,c('V1','V3','V4')]
data$V3<-sub("\\D*$","",as.vector(as.matrix(data$V3)))
data$V4<-sub("\\D*$","",as.vector(as.matrix(data$V4)))
#using regular expression to change the location
names(data) <- c('date','lan','lon')
data$lan <- as.numeric(data$lan)
data$lon <- -as.numeric(data$lon)
data$date <- as.Date(data$date,"%Y-%m-%d")
#why did you do that?
ggmap(get_googlemap(location="united states", zoom=4,maptype='terrain'),extent='device')+
  geom_point(data=data,aes(x=lon,y=lan),colour = 'red',alpha=0.7)+stat_density2d(aes(x=lon,y=lan,fill=..level..,alpha=..level..),
               size=2,bins=4,data=data,geom='polygon')+theme(legend.position = "none")

plotfunc <- function(x) {
  df <- subset(data,date <= x)
  df$lan <- as.numeric(df$lan)
  df$lon <- as.numeric(df$lon)
  p <- ggmap(get_googlemap(location="united states", zoom=4,maptype='terrain'),extent='device')+
    geom_point(data=df,aes(x=lon,y=lan),colour = 'red',alpha=0.7)
}
time <- sort(unique(data$date))
saveGIF(for( i in time) print(plotfunc(i)))





That's it. 



Reference:


2014年9月8日星期一

When you want to combine your mac disk, look here!



When I got my first mac, I set two disk for it. Several month passed, the top disk is nearly used up. Thus I want to combine it. Here is the way to combine the them.






First:Application---->Utility------->Disk Utility
Second:Copy the files in the unwanted disks 
Third:Deleted unwanted disks and resize the remain ones.(details in picture above)


That's it. Have a good day!