Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created March 14, 2012 03:43
Show Gist options
  • Save timelyportfolio/2033872 to your computer and use it in GitHub Desktop.
Save timelyportfolio/2033872 to your computer and use it in GitHub Desktop.
japan trade with korea.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)
numgeog <- 8 #there are really 9 but the last is "special area"
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)
}
#focus on Korea, so i will be 1 for Asia
i=1
#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
geogdata.xts<-merge(as.xts(geogdata[[i]][,2:NCOL(geogdata[[i]])],order.by=xtsdate))
geogdata.xts.Korea<-merge(geogdata.xts[,3:4],geogdata.xts[,3]-geogdata.xts[,4])
#eliminate trailing zeros
geogdata.xts.Korea <- geogdata.xts.Korea[which(geogdata.xts.Korea[,2]!=0),]
plot.zoo(geogdata.xts.Korea,screens=c(1,1,2))
getSymbols("EXKOUS",src="FRED")
getSymbols("EXJPUS",src="FRED")
KRWJPY <- EXJPUS/EXKOUS #since these are expressed in denominator, reverse
trade.Korea <- na.omit(merge(geogdata.xts.Korea,KRWJPY))
colnames(trade.Korea)<-c("Exports","Imports","SurplusDeficit","KRWJPY")
#panel.special function to better control the plot
panel.special <- function(col="grey30",...){
#to debug print(as.list(parent.frame()))
#set up titles for each of the panels in the graph
titles<-c("Imports and Exports",NA,"Surplus or Deficit","KRW/JPY")
#get the data so we can use for axis labelling
plotdata <- parent.frame()$x
#this will tell us the panel number or screens so we can customize
panel.number <- parent.frame()$panel.number
#this will tell us the number of the column
n.data <- parent.frame()$i
if (panel.number==1){ #for the import and export data
lines(col=c("steelblue2","steelblue4")[n.data],...)
#draw line for the x axis
abline(h=par()$usr[3],col="black")
} else { #for the surplus/deficit and korean won/yen
lines(col="grey70",...)
}
if (panel.number==2) { #mark the 0 on surplus/deficit
#draw horizontal line at zero
abline(h=0,col="grey30")
axis(side=2,at=0,lwd=0,lwd.ticks=1,las=1,cex.axis=0.8)
}
if (panel.number==3){ #add the date x axis on the last panel
#draw line for the x axis
abline(h=par()$usr[3],col="black")
#do x axis
axis(side=1,
at=c(index(plotdata)[1],pretty(index(plotdata))),
labels=format(c(index(plotdata)[1],pretty(index(plotdata))),"%Y"),
lwd=0,lwd.ticks=1)
}
#label each of the panels
title(main=titles[n.data],adj=0, line=-1,col.main="grey30",cex.main=0.85)
}
plot.zoo(trade.Korea,
screens=c(1,1,2,3),
ylab=NA,xlab=NA,
yaxt="n",xaxt="n",
bty="n",
main="Japan Trade with Korea and the Won/Yen",col.main="grey30",
panel=panel.special,
)
#get US trade data with Korea and Japan
library(XML)
url.japan<-"http://www.mac.doc.gov/japan-korea/statistics/us-japan-trade.htm"
url.korea<-"http://www.mac.doc.gov/japan-korea/statistics/us-Korea-trade.htm"
us.japan<-readHTMLTable(url.japan,skip.rows=1,header=TRUE)[[2]]
us.korea<-readHTMLTable(url.korea,skip.rows=1,header=TRUE)[[2]]
#combine into one xts object
us.trade <- cbind(us.japan[,2:4],us.korea[,2:4])
#erase commas
col2cvt <- 1:NCOL(us.trade)
us.trade[,col2cvt] <- lapply(us.trade[,col2cvt],
function(x){as.numeric(gsub(",", "", x))})
us.trade.xts <- as.xts(us.trade,order.by=as.Date(paste(us.japan[,1],"-12-31",sep="")))
colnames(us.trade.xts)<-c("Japan.Exports","Japan.Imports","Japan.Trade","Korea.Exports","Korea.Imports","Korea.Trade")
plot.zoo(us.trade.xts[,c(3,6)],
screens=1,
col=c("steelblue2","indianred3"),
lwd=3,
bty="n",
yaxt="n",
xaxt="n",
ylab="US$ Billion",
xlab=NA)
#draw horizontal lines at each breakpoint
abline(h=pretty(c(0,min(us.trade.xts[,3]))),col="grey70")
#draw line for the x axis
abline(h=par()$usr[3],col="black")
#do x axis
axis(side=1,
at=c(index(us.trade.xts)[1],pretty(index(us.trade.xts))),
labels=format(c(index(us.trade.xts)[1],pretty(index(us.trade.xts))),"%Y"),
lwd=0,lwd.ticks=1)
#do y axis
axis(side=2,
at=pretty(c(0,min(us.trade.xts[,3]))),
labels=pretty(c(0,min(us.trade.xts[,3])))/1000,
lwd=0,las=1)
title(main="US Trade Deficit with Japan and Korea",adj=0, col.main="grey30")
#add a legend
legend("right",c("Korea","Japan"),lwd=3,lty=1,col=c("indianred3","steelblue2"),bty="n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment