Data
We got the data from UNICEF website “State of the World children 2015 Tanzania Statistical table”
library(knitr)
data<-read.csv("C:/Users/User/Documents/R/Project Life.csv",head=T)
data
## Age.Group X2013..Female X2013..Male X2012..Female X2012..Male
## 1 1 100000.0 100000.0 100000.0 100000.0
## 2 5 96640.0 96070.0 96520.0 95950.0
## 3 10 95186.6 94455.7 94973.6 94232.3
## 4 15 93821.6 92833.8 93514.2 92514.2
## 5 20 92808.2 91731.6 92424.2 91336.3
## 6 25 91718.0 90266.4 91227.4 89762.9
## 7 30 90301.7 88432.8 89728.9 87884.6
## 8 35 88247.9 86102.4 87257.0 85325.2
## 9 40 85827.7 83232.5 84268.2 82042.1
## 10 45 82838.0 79502.8 80414.6 77640.7
## 11 50 79889.9 75637.7 76936.6 73227.7
## 12 55 77121.9 71881.5 73857.2 69094.8
## 13 60 73957.8 67799.4 70604.7 64911.0
## 14 65 70146.9 62972.0 66832.0 60056.8
## 15 70 64741.0 56597.5 61565.6 53792.7
## 16 75 56863.9 48182.1 53954.0 45651.8
## 17 80 45786.3 37363.9 43306.5 35260.8
## 18 85 31914.1 24691.3 30057.1 23184.6
## 19 90 16005.2 11628.8 15227.1 10889.9
## 20 95 5709.2 3569.5 5227.3 3276.2
## 21 100 1200.9 575.4 1032.4 511.0
## 22 105 126.9 38.6 97.7 32.3
## X2000..Female X2000..Male X1990..Female X1990..Male
## 1 100000.0 100000.0 100000.0 100000.0
## 2 92380.0 91560.1 90429.9 89409.9
## 3 87419.5 86319.9 84085.1 82727.0
## 4 85049.8 83833.1 81393.1 79873.9
## 5 83754.7 82531.5 80013.9 78480.6
## 6 82345.9 81025.1 78436.0 76828.8
## 7 79844.2 78600.9 76262.9 74319.6
## 8 73877.8 74389.9 73530.5 71379.1
## 9 68171.8 69255.6 70764.0 68278.3
## 10 62929.9 63389.7 67887.4 64954.9
## 11 59028.7 58462.6 64908.8 61480.1
## 12 55304.5 54128.7 61671.1 57801.8
## 13 51600.2 50054.5 58074.7 53450.2
## 14 47951.7 45028.8 53767.3 48042.3
## 15 43245.6 39064.2 47948.1 41585.1
## 16 36727.1 32125.0 40053.2 33618.6
## 17 28153.4 23716.1 29976.4 24076.1
## 18 18351.4 14614.2 18891.4 14188.3
## 19 8572.7 6234.9 8434.5 5676.3
## 20 2655.6 1654.2 2480.9 1386.7
## 21 474.5 224.8 421.9 172.1
## 22 42.1 13.1 36.5 9.6
To import the data to RMarkdown
data1<-read.csv("C:/Users/User/Documents/R/Project Life.csv",head=T, stringsAsFactors = FALSE)
data1
## Age.Group X2013..Female X2013..Male X2012..Female X2012..Male
## 1 1 100000.0 100000.0 100000.0 100000.0
## 2 5 96640.0 96070.0 96520.0 95950.0
## 3 10 95186.6 94455.7 94973.6 94232.3
## 4 15 93821.6 92833.8 93514.2 92514.2
## 5 20 92808.2 91731.6 92424.2 91336.3
## 6 25 91718.0 90266.4 91227.4 89762.9
## 7 30 90301.7 88432.8 89728.9 87884.6
## 8 35 88247.9 86102.4 87257.0 85325.2
## 9 40 85827.7 83232.5 84268.2 82042.1
## 10 45 82838.0 79502.8 80414.6 77640.7
## 11 50 79889.9 75637.7 76936.6 73227.7
## 12 55 77121.9 71881.5 73857.2 69094.8
## 13 60 73957.8 67799.4 70604.7 64911.0
## 14 65 70146.9 62972.0 66832.0 60056.8
## 15 70 64741.0 56597.5 61565.6 53792.7
## 16 75 56863.9 48182.1 53954.0 45651.8
## 17 80 45786.3 37363.9 43306.5 35260.8
## 18 85 31914.1 24691.3 30057.1 23184.6
## 19 90 16005.2 11628.8 15227.1 10889.9
## 20 95 5709.2 3569.5 5227.3 3276.2
## 21 100 1200.9 575.4 1032.4 511.0
## 22 105 126.9 38.6 97.7 32.3
## X2000..Female X2000..Male X1990..Female X1990..Male
## 1 100000.0 100000.0 100000.0 100000.0
## 2 92380.0 91560.1 90429.9 89409.9
## 3 87419.5 86319.9 84085.1 82727.0
## 4 85049.8 83833.1 81393.1 79873.9
## 5 83754.7 82531.5 80013.9 78480.6
## 6 82345.9 81025.1 78436.0 76828.8
## 7 79844.2 78600.9 76262.9 74319.6
## 8 73877.8 74389.9 73530.5 71379.1
## 9 68171.8 69255.6 70764.0 68278.3
## 10 62929.9 63389.7 67887.4 64954.9
## 11 59028.7 58462.6 64908.8 61480.1
## 12 55304.5 54128.7 61671.1 57801.8
## 13 51600.2 50054.5 58074.7 53450.2
## 14 47951.7 45028.8 53767.3 48042.3
## 15 43245.6 39064.2 47948.1 41585.1
## 16 36727.1 32125.0 40053.2 33618.6
## 17 28153.4 23716.1 29976.4 24076.1
## 18 18351.4 14614.2 18891.4 14188.3
## 19 8572.7 6234.9 8434.5 5676.3
## 20 2655.6 1654.2 2480.9 1386.7
## 21 474.5 224.8 421.9 172.1
## 22 42.1 13.1 36.5 9.6
Structure of the data
str(data1)
## 'data.frame': 22 obs. of 9 variables:
## $ Age.Group : int 1 5 10 15 20 25 30 35 40 45 ...
## $ X2013..Female: num 100000 96640 95187 93822 92808 ...
## $ X2013..Male : num 100000 96070 94456 92834 91732 ...
## $ X2012..Female: num 100000 96520 94974 93514 92424 ...
## $ X2012..Male : num 100000 95950 94232 92514 91336 ...
## $ X2000..Female: num 100000 92380 87420 85050 83755 ...
## $ X2000..Male : num 100000 91560 86320 83833 82532 ...
## $ X1990..Female: num 100000 90430 84085 81393 80014 ...
## $ X1990..Male : num 100000 89410 82727 79874 78481 ...
sapply(data1,mode)
## Age.Group X2013..Female X2013..Male X2012..Female X2012..Male
## "numeric" "numeric" "numeric" "numeric" "numeric"
## X2000..Female X2000..Male X1990..Female X1990..Male
## "numeric" "numeric" "numeric" "numeric"
Extract 2013 Male and Female Survivals
Femal<-data1$X2013..Female
Mal<-data1$X2013..Male
Tanzania<-data.frame(Year,Femal,Mal)
Tanzania
## Year Femal Mal
## 1 1 100000.0 100000.0
## 2 5 96640.0 96070.0
## 3 10 95186.6 94455.7
## 4 15 93821.6 92833.8
## 5 20 92808.2 91731.6
## 6 25 91718.0 90266.4
## 7 30 90301.7 88432.8
## 8 35 88247.9 86102.4
## 9 40 85827.7 83232.5
## 10 45 82838.0 79502.8
## 11 50 79889.9 75637.7
## 12 55 77121.9 71881.5
## 13 60 73957.8 67799.4
## 14 65 70146.9 62972.0
## 15 70 64741.0 56597.5
## 16 75 56863.9 48182.1
## 17 80 45786.3 37363.9
## 18 85 31914.1 24691.3
## 19 90 16005.2 11628.8
## 20 95 5709.2 3569.5
## 21 100 1200.9 575.4
## 22 105 126.9 38.6
To convert Female Survivals into percentage
Femal
## [1] 100000.0 96640.0 95186.6 93821.6 92808.2 91718.0 90301.7
## [8] 88247.9 85827.7 82838.0 79889.9 77121.9 73957.8 70146.9
## [15] 64741.0 56863.9 45786.3 31914.1 16005.2 5709.2 1200.9
## [22] 126.9
str(Femal)
## num [1:22] 100000 96640 95187 93822 92808 ...
Female<-(Femal*100)/100000
Female<-round(Female,digits = 0)
Female
## [1] 100 97 95 94 93 92 90 88 86 83 80 77 74 70 65 57 46
## [18] 32 16 6 1 0
To convert Male Survivals into percentage
Male<-(Mal*100)/100000
Male<-round(Male,digits = 0)
Male
## [1] 100 96 94 93 92 90 88 86 83 80 76 72 68 63 57 48 37
## [18] 25 12 4 1 0
Data frame for percentage of survivals Male and Female
Tanzania1<-data.frame(Year,Female,Male)
Tanzania1
## Year Female Male
## 1 1 100 100
## 2 5 97 96
## 3 10 95 94
## 4 15 94 93
## 5 20 93 92
## 6 25 92 90
## 7 30 90 88
## 8 35 88 86
## 9 40 86 83
## 10 45 83 80
## 11 50 80 76
## 12 55 77 72
## 13 60 74 68
## 14 65 70 63
## 15 70 65 57
## 16 75 57 48
## 17 80 46 37
## 18 85 32 25
## 19 90 16 12
## 20 95 6 4
## 21 100 1 1
## 22 105 0 0
Plot
plot(Female~Year,Tanzania1,ann=T,type= "l",lwd=2, col="red")
title(main = "Female Survival Function")
xf<-c(Year,1)
yf<-c(Female,0)
polygon(xf,yf, density=15,angle = 135)
plot(Male~Year,Tanzania1,ann=T,type="l",lwd=2, col="blue")
abline(v=c(0,105),lty=2)
title(main = "Male Survival Function")
xm<-c(Year,1)
ym<-c(Male,0)
polygon(xm,ym, density=15,angle = 135)
plot(Female~Year,Tanzania1,ann=F,type= "l",lwd=2,col="red")
lines(Male~Year,Tanzania1,ann=F,type="l",lwd=2,col="blue")
title(main = "Comparizon between Tanzania Life Expectacy \n of Male and Female",xlab = "Years", ylab = "survival rate in %")
text(x=c(60,80),y=c(60,80), labels = c("Male","Female"))
plot(Female~Year,Tanzania1,ann=F,type= "l",lwd=2,col="red")
lines(Male~Year,Tanzania1,ann=F,type="l",lwd=2,col="blue")
title(main = "Comparizon between Tanzania Life Expectacy \n of Male and Female",xlab = "Years", ylab = "survival rate in %")
text(x=c(60,80),y=c(60,80), labels = c("Male","Female"))
x<-c(xm,rev(xf))
y<-c(ym,rev(yf))
Tanzania2<-data.frame(x,y)
polygon(Tanzania2,angle=45,col="gray")
Grammer of graphics plot(ggplot)
library(ggplot2)
(g1<-ggplot() + geom_line(data=Tanzania1, aes(x=Year, y=Female, color="red")))
(g2<-g1 + geom_line(data=Tanzania1,aes(x=Year, y=Male,color="blue")))
(g3<- g2 + scale_color_discrete(name = ("Indicator Group"), breaks = c("red","blue"),labels = c("Female", "Male")))
g4<-g3 +
theme(legend.position = c(0.5,0.5))
(g5<- g4 + theme_bw())
(P1<-g5 + geom_polygon(data=Tanzania2,aes(x = x, y = y), alpha = 0.7, fill = "grey"))
(P2<-P1 +
xlab("Years") + ylab("SUrvival rate %") +
ggtitle("Comparizon between Tanzania Life Expectacy \n of Male and Female"))
(P3<-P2 +
annotate("text", x=c(62,75), y= c(62,62), label=c("Male", "Female")))