library(maptools) library(pyramid) library(rgdal) # Kobe extracted by EpiMap's AddLayerPartial from Hyogo of Kokudo-chiriin kobe <- readOGR("./kobe-city.shp") kobedata <- kobe @ data cents <- coordinates(kobe) geo <- data.frame(x=cents[,1], y=cents[,2]) geo$areas <- sapply(slot(kobe,"polygons"),slot,"area") geo$JCODE <- kobedata$N03_007 pop <- read.delim("http://minato.sip21c.org/ldaR/2010kobepop-sex-ageby5.txt", fileEncoding="CP932") # pdf("kobe-city-with-population-pyramids.pdf", width=12, height=9) # par(family="Japan1GothicBBB", mar=c(6,1,5,1), cex=0.5) plot(kobe, col=NA, xlab="", ylab="", axes=FALSE) gpa <- par()$usr cent2frame <- function(x, y, sizex=(gpa[2]-gpa[1])/20, sizey=(gpa[4]-gpa[3])/15) { return(c(x-sizex, x+sizex, y-sizey, y+sizey)) } for (i in names(table(kobedata$N03_007))) { ku <- subset(geo, JCODE==i) j <- which.max(ku$areas) kupop <- subset(pop, JCODE==i) pyramidf(kupop[,1:3], frame=cent2frame(ku[j,1]-ifelse(i==28105, 0.01, 0), ku[j,2]+ifelse(i==28105, 0.04, ifelse(i==28102, 0.03, 0))), Cstep=2, Csize=0.6, Cadj=-0.07, Cgap=0.8, Lcol="skyblue", Rcol="pink", GL=FALSE, Llab="’j", Rlab="—", Clab="", Laxis=0:2*5000, main=kupop$Ward[1], AxisFM="d") } title(main="Population pyramids of each ward in Kobe city, 2010", sub="Geo-data source: Geospatial Infomation Authority of Japan.", cex.main=2, cex.sub=1.5) # dev.off()