Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created March 12, 2012 19:31
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 timelyportfolio/2024186 to your computer and use it in GitHub Desktop.
Save timelyportfolio/2024186 to your computer and use it in GitHub Desktop.
japan trade by region.r
#further explore Japanese trade data by region
#website is http://www.customs.go.jp/toukei/suii/html/time_e.htm
#format is http://www.customs.go.jp/toukei/suii/html/data/d42ma001.csv
#and the filename increments from 001 to 008 for the geographic areas
require(quantmod)
require(reshape)
require(lattice)
require(latticeExtra)
numgeog <- 8
urls <- paste("http://www.customs.go.jp/toukei/suii/html/data/d42ma00",
1:numgeog,".csv",sep="")
geogdata <- vector("list",numgeog)
for (i in 1:numgeog) {
#read the csv file with geographic area trade data
geogdata[[i]] <- read.csv(urls[i],stringsAsFactors=FALSE,skip=3)
#get date in usable form yyyy-mm-dd
origdate <- geogdata[[i]][,1]
xtsdate <- as.Date(paste(substr(origdate,0,4),substr(origdate,6,8),"01",sep="-"))
#get one xts with summay geographic area data
ifelse(i==1,geogdata.xts<-merge(as.xts(geogdata[[i]][,2:3],order.by=xtsdate),as.xts(geogdata[[i]][,2]-geogdata[[i]][,3],order.by=xtsdate)),
geogdata.xts<-merge(geogdata.xts,merge(as.xts(geogdata[[i]][,2:3],order.by=xtsdate),as.xts(geogdata[[i]][,2]-geogdata[[i]][,3],order.by=xtsdate))))
}
#set up labels for geographic areas
geogarea <- c("Asia","Oceania","NorthAmerica","SouthAmerica","WestEurope","EastEurope","MiddleEast","Africa")
#name columns by appending geographic area with "Exports" and "Imports"
colnames(geogdata.xts)[seq(from=1,to=numgeog*3,by=3)]<-paste(geogarea,"Exports",sep="")
colnames(geogdata.xts)[seq(from=2,to=numgeog*3,by=3)]<-paste(geogarea,"Imports",sep="")
colnames(geogdata.xts)[seq(from=3,to=numgeog*3,by=3)]<-paste(geogarea,"SurpDef",sep="")
#melt data for lattice plotting
geogdata.melt <- melt(as.data.frame(cbind(index(geogdata.xts),coredata(geogdata.xts)),
stringsAsFactors=FALSE),
id.vars=1)
#get melted data into form -- date, geographic area, direction of trade, and amount
geogdata.melt <- as.data.frame(cbind(format(index(geogdata.xts),"%Y-%m-%d"),
substr(geogdata.melt[,2],1,nchar(as.vector(geogdata.melt[,2]))-7),
substr(geogdata.melt[,2],nchar(as.vector(geogdata.melt[,2]))-6,nchar(as.vector(geogdata.melt[,2]))),
geogdata.melt[,3]),stringsAsFactors=FALSE)
#convert to Date object
geogdata.melt[,1] <- as.Date(geogdata.melt[,1])
#get strings as numeric since I do not know any better way to preserve numeric
geogdata.melt[,4] <- as.numeric(geogdata.melt[,4])
#data comes with 0s filling remainder of months left in the year
geogdata.melt <- geogdata.melt[which(geogdata.melt[,4]!=0),]
colnames(geogdata.melt)<-c("Date","Area","Direction","Amount")
#add alpha to colors
addalpha <- function(cols,alpha=180) {
rgbcomp <- col2rgb(cols)
rgbcomp[4] <- alpha
return(rgb(rgbcomp[1],rgbcomp[2],rgbcomp[3],rgbcomp[4],maxColorValue=255))
}
xyplot(Amount~Date|factor(Area),groups=Direction,data=geogdata.melt,
type="l",lwd=0.5,
layout=c(8,1),
col=apply(as.matrix(c("steelblue4","steelblue2","gray70")),MARGIN=1,addalpha),
par.settings = theEconomist.theme(box = "transparent"),
scales=list(x=list(alternating=c(1,0),draw=TRUE,
at=as.Date(index(geogdata.xts)[c(1,NROW(geogdata.xts))]),
labels=format(as.Date(index(geogdata.xts)[c(1,NROW(geogdata.xts))]),"%Y"))),
auto.key = list(space = "right",col=apply(as.matrix(c("steelblue4","steelblue2","gray70")),MARGIN=1,addalpha,alpha=210),
points=FALSE))+
layer(panel.abline(h=0, col="black"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment