library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(dplyr)
library(ggplot2)
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
# survey and votes
pg_votes <- read.csv("Data_sets/Prince George Citizen Budget 2023/PG - survey and votes - clean.csv")
# decision users
pg_users <- read.csv("Data_sets/Prince George Citizen Budget 2023/princegeorge2023_decision_users.csv")
# voting summary
pg_vote_summary <- read.csv("Data_sets/Prince George Citizen Budget 2023/princegeorge2023-voting-summary.csv")
# scenario comparison
pg_comparison <- read.csv("Data_sets/Prince George Citizen Budget 2023/princegeorge2023-scenario-comparison.csv")
{r} {r, include=FALSE} # view(pg_votes) # view(pg_users) # view(pg_vote_summary) # view(pg_comparison)
# remove staff/demo users
pg_users <- pg_users %>% filter(!Roles %in% c("Staff, Decision Admin, Demo", "Decision Admin, Demo", "Guest, Demo, Participant", "Staff, Participant"))
unwanted <- pg_users %>%
filter(Roles %in% c("Staff, Decision Admin, Demo", "Decision Admin, Demo", "Guest, Demo, Participant", "Staff, Participant")) %>%
select(Id.Number)
# remove cols in pg_votes with all NA
drop <- c("Email", "Name")
pg_votes <- pg_votes[, !(names(pg_votes) %in% drop)]
# remove % symbols in cells
pg_vote_summary$Consensus <- as.numeric(gsub("%", "", as.character(pg_vote_summary$Consensus)))
pg_vote_summary$Support <- as.numeric(gsub("%", "", as.character(pg_vote_summary$Support)))
pg_vote_summary$Conflict <- as.numeric(gsub("%", "", as.character(pg_vote_summary$Conflict)))
pg_vote_summary$Approval <- as.numeric(gsub("%", "", as.character(pg_vote_summary$Approval)))
# participation rate
temp.df <- data.frame()
temp.df <- filter(pg_users, !Voting.Complete == 0.00 & !is.na(Voting.Complete))
participants <- nrow(temp.df)
users <- nrow(pg_users[!is.na(pg_users$Name),])
participation_rate <- participants/users
comments <- sum(na.omit(pg_users$Comment.Count))
graph_by_topic <- function(topic){
temp.df <-
pg_vote_summary %>%
filter(Topic %in% topic)
temp.df <- temp.df[-1,]
# bar graph
ggplot(temp.df, aes(x = fct_inorder(Option), y = Slider.Votes)) +
geom_bar(
position = 'dodge',
stat= "identity",
fill = "#80b57f",
width = 0.7) +
# bar count
geom_text(
size = 3.5,
aes(label = Slider.Votes),
hjust = 1.5,
vjust=0.4,
position = position_dodge(width = 0.5)) +
# flipping coordinates
coord_flip() +
# labeling
labs(
title = topic,
x = "Vote Selection",
y = "Number of Votes"
)
}
Example of me using the function with Fire Protection Services and Police Services.
graph_by_topic("Fire Protection Services")
graph_by_topic("Police Services")
Don’t know how to make it clockwise.
# Topic column
donut <-
pg_vote_summary %>%
distinct(Topic)
# Weight column
donut$Weight <- na.omit(pg_vote_summary$Average.Weighting)
# Percentages column
donut$Percentages <- round((donut$Weight / sum(donut$Weight)*100), 1)
# size of donut
hsize <- 2
donut <- donut %>%
mutate(x = hsize)
# plot
ggplot(donut, aes(x = hsize, y = Weight, fill = Topic)) +
geom_col() +
geom_text(size = 2.5, aes(label = paste(Percentages, "%")),
position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y") + #hmm
scale_fill_brewer(palette = "Set3") +
xlim(c(0.2, hsize + 0.5)) + #hmm
theme(panel.background = element_rect(fill = "white"),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank())
prop.type <- data.frame()
prop.type <- pg_votes %>%
count(Property.Type)
slices <- prop.type$n[2:3]
lbls <- prop.type$Property.Type[2:3]
pie(slices, labels = lbls, main = "Property Type")
# data frame for satisfaction columns
satisfy <- pg_votes[grepl("Satisfaction",colnames(pg_votes))]
# vector of column names for satisfaction
satisfy.vec <- colnames(pg_votes[grepl("Satisfaction",colnames(pg_votes))])
# by law enforcement
ggplot(satisfy, aes(x = Satisfaction.Bylaw.Enforcement)) +
geom_bar(
fill = "#89a4d6",
width = 0.8) +
scale_x_continuous(breaks = c(0:10))
## Warning: Removed 436 rows containing non-finite values (stat_count).
# mean satisfaction for each satisfaction column
mean.satisfy <- c()
for (elem in satisfy.vec){
mean.satisfy <- append(mean.satisfy, mean(na.omit(satisfy[, elem])))
print(elem)
print(mean(na.omit(satisfy[, elem])))
}
## [1] "Satisfaction..Roads.and.Sidewalks.Services"
## [1] 5.180974
## [1] "Satisfaction.Bylaw.Enforcement"
## [1] 3.97561
## [1] "Satisfaction.Community.Planning.and.Infrastructure"
## [1] 3.907463
## [1] "Satisfaction.Community.Support.services"
## [1] 4.454545
## [1] "Satisfaction.Corporate.Services"
## [1] 3.712121
## [1] "Satisfaction.Fire.Protection"
## [1] 7.417874
## [1] "Satisfaction.Parks..Trails..and.Beautification"
## [1] 5.777202
## [1] "Satisfaction.Police.Services"
## [1] 4.935417
## [1] "Satisfaction.Public.Transit"
## [1] 4.612805
## [1] "Satisfaction.Recreation.and.Community.Services"
## [1] 5.714715
## [1] "Satisfaction.Services.that.attract.and.retain.business.and.facilitate.development"
## [1] 4.164474
## [1] "Satisfaction.Snow.and.Ice.Control"
## [1] 5.939732