-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathva_cs_webr.qmd
134 lines (115 loc) · 5.76 KB
/
va_cs_webr.qmd
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
```{webr-r}
#data setup
{{< include data_setup.R >}}
#ANALYSIS TOGGLE
######################################
{{< include re_report.R >}}
{{< include execute.R >}}
{{< include toggle.R >}}
#programs to remove per the CWC report
{{< include rm_pgms.R >}}
#deduplicate across all columns
{{< include dedup.R >}}
#clean program names
{{< include roster_clean.R >}}
##create dataset of numerators and denominators
#recidivism rates overall
{{< include rates.R >}}
#clean program names
{{< include staff_clean.R >}}
#create table dataset
{{< include tabledata.R >}}
#manage the data to produce recidivism rates
{{< include cwc_unw.R >}}
#manage the data to produce recidivism rates
#total clients served (all years, year1, year2)
{{< include cwc_w.R >}}
```
```{webr-r}
#| context: interactive
#| editor-max-height: 500
#| editor-code-line-numbers: 4-5, 8-12
#build bar chart of recidivism rates across programs
#in the block below, change the values to update the plot as needed
###############################
ALL.BY <- F #F overall years; T by year
CWC <- F #T remove 5 programs; F keep 10 programs
#plot colors
staffc <- "brown" #set color for staffing text
hlinew1 <- "orange" #set color for weighted line
hlinew2 <- "darkgrey" #set color for unweighted line
date1c <- "deepskyblue1" #set color for earlier data date/overall
date2c <- "darkolivegreen3" #set color for later data date
###############################
#filter or don't filter out programs for plotting
tabout.cwc <- tabout
invisible({if(CWC) tabout.cwc <- tabout |> filter(! programs_clean %in% rm.pgms)})
#years of data do you want to plot
dates <- as.numeric(c(date1,date2))
#custom title header of plot
titledates <- ifelse(length(dates)>=2 & date1 != date2, paste0(date1," - ",date2),
ifelse((dates==date1 | dates==date2) & ALL.BY, as.character(dates),
ifelse(!ALL.BY, date1, "")))
#which years/programs are missing data? (footer)
prg.NA <- tabout.cwc |>
filter(is.na(recid_rate_year)) |>
pull(program_official)
##plot it! this will plot recidivism rates with overlaid staffing text
rr <- ggplot(tabout.cwc |>
filter(if(ALL.BY) year %in% dates else year == date2) |>
mutate(recid_rate = case_when(ALL.BY ~ recid_rate_year,
!ALL.BY ~ recid_rate_all))
,aes(x=str_wrap(program_official, 32), y=recid_rate, fill=year)) +
geom_bar(position = "dodge",stat = "identity") +
geom_text(aes(label=ifelse(year==dates[2],paste(num_staff,"staff"),"")), vjust=-0.3, color = staffc) +
scale_fill_manual(values=c(date1c,date2c)) +
ylim(0,1) +
ylab("Recidivism Rate") +
xlab("EBRR Programs") +
ggtitle(paste0("Recidivism Rates across EBRR programs\n",titledates)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 30, vjust = 1, hjust=1)) +
#remove legend if plotting overall (not by year)
{if(!ALL.BY) theme(legend.position="none")}+
#only print caption if a program is missing data
{if(length(prg.NA)!=0) labs(caption = capture.output(cat("The following programs were missing data in some years:", unique(toupper(prg.NA)), sep=" ")))}+
theme(plot.caption=element_text(hjust=0))
#which weights to plot
w <- if(ALL.BY & !CWC) c(w.d1,w.d2) else if(ALL.BY & CWC) {c(w.d15,w.d25)} else if(!ALL.BY & !CWC) {w.a} else {w.a5}
unw <- if(ALL.BY & !CWC) c(unw.d1,unw.d2) else if(ALL.BY & CWC) {c(unw.d15,unw.d25)} else if(!ALL.BY & !CWC) {unw.a} else {unw.a5}
#nudging labels based on number of programs in plot
nudge.w <- 0
nudge.unw <- 0
invisible({if(!CWC) (nudge.w <- -4) & (nudge.unw <- -8)})
#add recidivism weight averages to plot
rrfinal <- rr +
geom_hline(yintercept=w[1], linetype = "dashed", color = hlinew1, size = 1) +
{if(ALL.BY) geom_hline(yintercept=w[2], linetype = "dashed", color = hlinew1, size = 1)}+
geom_hline(yintercept=unw[1], linetype = "dashed", color = hlinew2, size = 1) +
{if(ALL.BY) geom_hline(yintercept=unw[2], linetype = "dashed", color = hlinew2, size = 1)}+
geom_label_repel(aes(label=lab, y=0.25), fill=hlinew1,
data = tabout.cwc |>
filter(if(ALL.BY) year %in% dates else year == date2) |>
mutate(recid_rate = case_when( ALL.BY ~ recid_rate_year,
!ALL.BY ~ recid_rate_all),
lab = case_when( ALL.BY & date1 != date2 ~ paste0(date1," Weighted avg: ",w[1],"\n",date2," Weighted avg: ",w[2]),
!ALL.BY & date1 != date2 ~ paste0(date1,"-",date2,"\nWeighted avg: ",w[1]),
ALL.BY & date1 == date2 ~ paste0(date1,"\nWeighted avg: ",w[1]))) |>
filter(programs_clean == last & year == date2),
#move unweighted label left or right on x axis
nudge_x = nudge.w,
min.segment.length = Inf) +
geom_label_repel(aes(label=lab, y=0.5), fill=hlinew2,
data = tabout.cwc |>
filter(if(ALL.BY) year %in% dates else year == date2) |>
mutate(recid_rate = case_when( ALL.BY ~ recid_rate_year,
!ALL.BY ~ recid_rate_all),
lab = case_when( ALL.BY & date1 != date2 ~ paste0(date1," Unweighted avg: ",unw[1],"\n",date2," Unweighted avg: ",unw[2]),
!ALL.BY & date1 != date2 ~ paste0(date1,"-",date2,"\nUnweighted avg: ",unw[1]),
ALL.BY & date1 == date2 ~ paste0(date1,"\nUnweighted avg: ",unw[1]))) |>
filter(programs_clean == last & year == date2),
#move unweighted label left or right on x axis
nudge_x = nudge.unw,
min.segment.length = Inf)
rrfinal
```