-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_2013.Rmd
More file actions
108 lines (93 loc) · 5.71 KB
/
update_2013.Rmd
File metadata and controls
108 lines (93 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
title: "South African Causes of Death, 2013"
resource_files:
- data.rds
runtime: shiny
output: html_document
---
## get data here: https://open.africa/dataset/statistics-south-africa-causes-of-death/resource/97d00e9d-b614-47b6-8eb4-58c7cfe6567a
```{r, echo=FALSE}
library(tibble)
mydata<-readRDS("data.rds")
library(ggplot2)
library(scales)
inputPanel(
selectInput("gender", label = "Sex:",
choices = c("male", "female", "unspecified", "any"), selected = "any"),
selectInput("nat_cause", label = "Cause of Death:",
choices = list("any",`Natural/Unnatural` =c("Natural Causes", "unnatural causes"), `detailed` =levels(as.factor(mydata$maincause))), selected = "any", multiple=TRUE),
selectInput("province", label = "Province:",
choices=c("any", levels(as.factor(mydata$province))) , selected = "any", multiple=TRUE),
selectInput("byvar", label = "Show Distribution by:",
choices = list("sex"="sex", "cause of death"="maincause", "natural/unnatural"="natural", "province"="province", "smoking status"="smoke"), selected = "sex"),
sliderInput("max_age", "Age Range:", 0, 100, c(1,100)),
checkboxInput("exclude", label = "Exclude unspecified sex and province", value = TRUE),
checkboxInput("exclude_unknown", label = "Exclude unknown causes of death", value = FALSE)
)
renderPlot({
datanew<-mydata[which(mydata$sex==input$gender|mydata$all==input$gender),]
datanew<-datanew[which(datanew$natural==input$nat_cause|datanew$all==input$nat_cause|datanew$maincause==input$nat_cause),]
datanew<-datanew[which(datanew$province==input$province|datanew$all==input$province),]
#include to subset by age for density plot#
datanew<-datanew[which(datanew$age>input$max_age[1] & datanew$age<input$max_age[2] ),]
if (input$exclude==TRUE) datanew<-datanew[which(datanew$province!="Unknown" & datanew$sex!="unspecified"),]
if (input$exclude_unknown==TRUE) datanew<-datanew[which(datanew$maincause!="Undiagnosed/unknown"),]
attach(datanew)
#plot(xlim=c(0,100), density(na.omit(datanew$age)))
library(sm)
layout(matrix(c(1,2,3),ncol=3), widths=c(12,3,1.2),heights=c(2.5,2.5,2.5))
par(mai=c(0.9, 0, 0.5, 0))
colfill<-c(2:(1+length(levels(as.factor(get(input$byvar))))))
#ltype=c(2:(1+length(levels(as.factor(get(input$byvar))))))
sm.density.compare(data=datanew,xlim=c(input$max_age[1],input$max_age[2]), age, group=as.factor(get(input$byvar)),lty = colfill, col=colfill)
title(main ="Age Distribution")
# attach(datanew)
#colfill<-c( 2:(2+length(levels(as.factor(get(input$byvar))))))
#ltype=c(2:(2+length(levels(as.factor(get(input$byvar))))))
#legend('topright', levels(as.factor(get(input$byvar))),lty = ltype, col=colfill,bty ="n")
#lines(density(datanew$age))
attach(datanew)
plot.new()
colfill<-c( 2:(1+length(levels(as.factor(get(input$byvar))))))
#ltype=c(2:(1+length(levels(as.factor(get(input$byvar))))))
legend('center', levels(as.factor(get(input$byvar))),lty = colfill, col=colfill,bty ="n", cex=1.5)
t<-as.matrix(table(as.factor(get(input$byvar))))
barplot(-t, beside=FALSE, col=colfill,density=colfill,border=colfill,lty=colfill, yaxt='n', main="Proportion of \n Deaths")
#######BAR graph
#ggplot(datanew, aes(x=datanew$all, fill = as.factor(get(input$byvar) )),col=colfill) +
# geom_bar( )+
# theme(legend.title = element_blank()) +
# ggtitle(paste("Cause of Death by Selected Variable") )
})
renderPlot({
datanew<-mydata[which(mydata$sex==input$gender|mydata$all==input$gender),]
datanew<-datanew[which(datanew$natural==input$nat_cause|datanew$all==input$nat_cause|datanew$maincause==input$nat_cause),]
datanew<-datanew[which(datanew$province==input$province|datanew$all==input$province),]
#include to subset by age for density plot#
datanew<-datanew[which(datanew$age>input$max_age[1] & datanew$age<input$max_age[2] ),]
if (input$exclude==TRUE) datanew<-datanew[which(datanew$province!="Unknown" & datanew$sex!="unspecified"),]
if (input$exclude_unknown==TRUE) datanew<-datanew[which(datanew$maincause!="Undiagnosed/unknown"),]
attach(datanew)
colfill<-c( 2:(1+length(levels(as.factor(get(input$byvar))))))
#ltype=c(2:(1+length(levels(as.factor(get(input$byvar))))))
t<-as.matrix(table(as.factor(get(input$byvar))))
xx<-barplot(t, beside=TRUE, col=colfill,density=colfill,border=colfill,lty=colfill,yaxt='n', main="Absolute numbers")
text(x = xx, y=0.9*t, label = t)
})
renderPlot({
datanew<-mydata[which(mydata$sex==input$gender|mydata$all==input$gender),]
datanew<-datanew[which(datanew$natural==input$nat_cause|datanew$all==input$nat_cause|datanew$maincause==input$nat_cause),]
datanew<-datanew[which(datanew$province==input$province|datanew$all==input$province),]
datanew<-datanew[which(datanew$age>input$max_age[1] & datanew$age<input$max_age[2] ),]
#datanew<-ifelse(input$exclude==TRUE, datanew[which(datanew$province!="Unknown" & datanew$sex!="unspecified"),],datanew)
if (input$exclude==TRUE) datanew<-datanew[which(datanew$province!="Unknown" & datanew$sex!="unspecified"),]
ggplot(datanew, aes(x = factor(get(input$byvar)))) +
geom_bar(aes(fill = ifelse(get(input$byvar)!=maincause, maincause, sex)), position="fill" ) +
scale_y_continuous(labels=percent) +
theme(legend.title = element_blank()) +
ggtitle(ifelse(get(input$byvar)!=maincause,"Cause of Death by Selected Variable", "Cause of Death by gender") ) +
theme(plot.title = element_text(lineheight=.8, face="bold",hjust = 0.5))+
xlab(input$byvar) +ylab("percent")
})
```
[Code](https://github.com/talicassidy/dataviz) [URL] (https://epifever.shinyapps.io/Mortality_2013_update/) and [Data](https://open.africa/dataset/statistics-south-africa-causes-of-death/resource/97d00e9d-b614-47b6-8eb4-58c7cfe6567a)