library(circlize)
orig = c("IE","GB","US","ES","FI","US","IE","GB")
dest = c("FI","FI","ES","ES","US","US","US","IE")
direc = c("IB","OB","RETURN","DOM","OB","DOM","RETURN","IB")
df = data.frame(orig, dest, direc, stringsAsFactors = FALSE)
col = c("IB" = "red",
"OB" = "blue",
"RETURN" = "orange",
"DOM" = "green")
#--- (i) arrows ---#
directional = c("IB" = -1,
"OB" = 1,
"RETURN" = 2,
"DOM" = 0)
#--- (ii) distance from Track Rim ---#
diffHeight = c("IB" = -0.04,
"OB" = 0.04,
"RETURN" = 0,
"DOM" = 0)
## Set Chord Parameters
chordDiagram(df[1:2], col = col[df[[3]]], directional = directional[df[[3]]],
direction.type = c("arrows+diffHeight"),
diffHeight = diffHeight[df[[3]]])
## Generate Legend
legend("bottomleft", pch = 15, legend = names(col), col = col)
## Clear Memory
circos.clear()
## Load Data
df0 <- read.csv("./Input/FDICircle7.1.csv", header = TRUE, stringsAsFactors = FALSE)
## Generate Data
orig = c(df0$Source)
dest = c(df0$Destination)
direc = c(df0$Sector)
## Generate Dataframe
df = data.frame(orig, dest, direc, stringsAsFactors = FALSE)
sector_col = c("Building materials" = "peru",
"Business machines & equipment" = "darkolivegreen2",
"Business services" = "magenta",
"Coal, oil & gas" = "black",
"Electronic components" = "blue",
"Engines & turbines" = "red",
"Financial services" = "purple",
"Industrial equipment" = "goldenrod1",
"Metals" = "lightblue1",
"Renewable energy" = "springgreen3",
"Transportation & warehousing" = "darkcyan")
Region = union(df$orig, df$dest)
gap.degree = rep(1.7, length(Region))
gap.degree[as.vector(cumsum(table(Region)))] = 1.7
par(bg = "snow2")
circos.par(canvas.ylim = c(-1,1),
canvas.xlim = c(-1,0.6),# edit canvas size
gap.after = gap.degree, # adjust gaps between regions
track.margin = c(0,0),# adjust bottom and top margin
#canvas.ylim= c(-1, -1),
#rack.margin = c(0.05, 0.05),
track.height =0.07)
## Set Chord Parameters
chordDiagram(df[1:2], col = sector_col[df[[3]]],
grid.col = c("North America" = "dodgerblue4",
"Latin America" = "darkolivegreen3",
"Western Europe" = "darkorchid4",
"Rest of Europe" = "lightsteelblue",
"M. East" = "darkgoldenrod3",
"Africa" = "darkgreen",
"Asia-Pacific" = "firebrick2"),
annotationTrack = "grid",
annotationTrackHeight = 0.08,
preAllocateTracks = 1,
directional = TRUE,
direction.type = c("arrows+diffHeight"),
diffHeight = -uh(3, "mm"),
link.arr.type = "big.arrow",
transparency = 0.1)
## Customize Labels
circos.trackPlotRegion(track.index = 1, panel.fun = function(x,y) {
xlim = get.cell.meta.data("xlim")
ylim = get.cell.meta.data("ylim")
sector.name = get.cell.meta.data("sector.index")
circos.text(mean(xlim), ylim[1]-1.1, sector.name,
col = "white", font = 7,
#adj = c(0, 0.5),
cex= 0.8, facing = "inside",
niceFacing = TRUE)
#circos.axis(h = "bottom", labels.cex = 0.0005,
#major.tick.percentage = 0.05,
#major.tick.length = 0.6,
#sector.index = sector.name,
#track.index = 5)
}, bg.border = NA)
## Generate Legend
legend(title =expression(bold(underline("Green FDI Sectors"))),
#title.adj = c(0,0,-2,-2),
"left",cex = 0.8,
legend = c("Building materials",
"Business machines\n & equipment",
"Business services",
"Coal, oil & gas",
"Electronic\n components",
"Engines & turbines",
"Financial services",
"Industrial equipment",
"Metals",
"Renewable energy",
"Transportation\n & warehousing"),
#xjust= 0,
yjust = 2, #title.adj= c(0,1),
y.intersp = 1, x.intersp = 1,
text.width = .45,lwd = 2,
col = sector_col, ncol=1, inset=0.01,
bty ="n")
## Clear Memory
circos.clear()