日本の人口 2014年9月1日時点


Data Source:Statistics Bureau http://www.stat.go.jp/english/index.htm

Sys.time()
## [1] "2014-09-24 16:59:32 JST"

Package

library(rgl)
library(ggplot2)
library(reshape2)
library(xtable)

Read Data

username<-Sys.info()['user']
folder.name<-c("R_Data_Read","R_image","R_Data_Write")
path<-paste("C:/Users/",username,"/Desktop",sep="")
for(fff in 1:length(folder.name)){
folder.path<-file.path(path,folder.name[fff])
if(file.exists(folder.path)==F){dir.create(folder.path)}
assign(paste("path",fff,sep=""),folder.path)
}
setwd(path1) #read
file.list<-list()
for(iii in 1:length(dir(path1))){
file.list[[iii]]<-read.table(file=dir(path1)[iii],header=TRUE,sep=",",as.is=T) # fill=T,na.string=""
print(head(file.list[[iii]],1))
cat("\n")
print(tail(file.list[[iii]],1))
cat("\n")
str(file.list[[iii]])
}
##   age.groups Both.sexes Male Female
## 1     0 ~ 4        522  268    254
## 
##    age.groups Both.sexes Male Female
## 21  100歳以上          6    1      5
## 
## 'data.frame':    21 obs. of  4 variables:
##  $ age.groups: chr  "0 ~ 4" "5 ~ 9" "10 ~ 14" "15 ~ 19" ...
##  $ Both.sexes: int  522 531 572 601 618 669 747 870 977 859 ...
##  $ Male      : int  268 272 293 308 318 342 379 441 494 432 ...
##  $ Female    : int  254 259 279 293 300 327 368 429 483 427 ...
setwd(path3)
dir(path1)
## [1] "05k2-1.csv"
summary(file.list)
##      Length Class      Mode
## [1,] 4      data.frame list
#file.select<-menu(dir(path1),title="File(0:Exit)")
file.select<-1
dataset<-file.list[[file.select]]

Result:Plot

## free ##
dataset[,1]<-factor(dataset[,1])
dataset[,1]<-ordered(dataset[,1],levels=dataset[,1][1:length(dataset[,1])])
levels(dataset[,1])
##  [1] "0 ~ 4"    "5 ~ 9"    "10 ~ 14"  "15 ~ 19"  "20 ~ 24" 
##  [6] "25 ~ 29"  "30 ~ 34"  "35 ~ 39"  "40 ~ 44"  "45 ~ 49" 
## [11] "50 ~ 54"  "55 ~ 59"  "60 ~ 64"  "65 ~ 69"  "70 ~ 74" 
## [16] "75 ~ 79"  "80 ~ 84"  "85 ~ 89"  "90 ~ 94"  "95 ~ 99" 
## [21] "100歳以上"
g<-ggplot(dataset,aes(x=age.groups,y=Both.sexes))
g<-g+geom_bar(stat="identity",fill="grey",alpha=0.5,color="black")
g+geom_text(aes(label=Both.sexes),hjust=0.5)+coord_flip() #vjust=-0.5

plot of chunk unnamed-chunk-4

g<-ggplot(dataset,aes(x=age.groups,y=Male))
g<-g+geom_bar(stat="identity",fill="grey",alpha=0.5,color="black")
g+geom_text(aes(label=Male),hjust=0.5)+coord_flip()

plot of chunk unnamed-chunk-4

g<-ggplot(dataset,aes(x=age.groups,y=Female))
g<-g+geom_bar(stat="identity",fill="grey",alpha=0.5,color="black")
g+geom_text(aes(label=Female),hjust=0.5)+coord_flip()

plot of chunk unnamed-chunk-4

dataset.melt<-melt(dataset,id.vars="age.groups",measure.vars=colnames(dataset)[3:4])
g<-ggplot(dataset.melt,aes(x=age.groups,y=value,group=variable,fill=variable))
g+geom_bar(stat="identity", position="dodge",alpha=0.5)+coord_flip()

plot of chunk unnamed-chunk-4

g+geom_bar(stat="identity",alpha=0.5)+coord_flip()

plot of chunk unnamed-chunk-4

g+geom_bar(stat="identity", position="fill",alpha=0.5)+coord_flip()

plot of chunk unnamed-chunk-4 Result:Table

ratio<-numeric()
for(nnn in 2:4){
tmp<-0
for(iii in seq(length(dataset[,1]),1,by=-1)){
tmp<-tmp+dataset[,nnn][iii]
ratio[iii]<-format(tmp/sum(dataset[,nnn])*100,digit=2) 
}
dataset<-cbind(dataset,ratio)
}
colnames(dataset)[5:7]<-c("Ratio:Both","Ratio:M","Ratio:F")
print(xtable(dataset),type="html")


age.groups Both.sexes Male Female Ratio:Both Ratio:M Ratio:F
1 0 ~ 4 522 268 254 100 100 100
2 5 ~ 9 531 272 259 96 96 96
3 10 ~ 14 572 293 279 92 91 92
4 15 ~ 19 601 308 293 87 87 88
5 20 ~ 24 618 318 300 82 82 83
6 25 ~ 29 669 342 327 78 76 79
7 30 ~ 34 747 379 368 72 71 74
8 35 ~ 39 870 441 429 66 65 68
9 40 ~ 44 977 494 483 60 58 62
10 45 ~ 49 859 432 427 52 50 54
11 50 ~ 54 777 389 388 45 43 48
12 55 ~ 59 766 381 386 39 36 42
13 60 ~ 64 904 443 460 33 30 36
14 65 ~ 69 910 439 472 26 23 29
15 70 ~ 74 792 368 423 19 16 21
16 75 ~ 79 626 276 350 13 9.9 15
17 80 ~ 84 486 194 292 7.6 5.4 9.6
18 85 ~ 89 305 103 203 3.7 2.3 5.1
19 90 ~ 94 130 31 99 1.3 0.62 2
20 95 ~ 99 35 6 29 0.32 0.11 0.52
21 100歳以上 6 1 5 0.047 0.016 0.077