# This program is available under a # BSD 3 clause license # Copyright 2021 B.K. Muite # # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # Install packages install.packages("tidyverse") # Load libraries library(tidyverse) # Read in data Kenya13to18 <- read_tsv("data/kenya-boost-2013-2018-reduce3.tsv", skip_empty_rows = TRUE, trim_ws = TRUE, na = c("-","NA"), col_names=TRUE) # View part of the data frame since it is large view(head(Kenya13to18,100)) names(Kenya13to18) # Remove rows with incomplete data Kenya13to18 <- drop_na(Kenya13to18) # Check resulting data head(Kenya13to18,5) # Get data types sapply(Kenya13to18,mode) # transform estimate to numeric Kenya13to18$`Final Budget (Approved Estimate)` <- as.numeric(as.character(Kenya13to18$`Final Budget (Approved Estimate)`)) # Check data types sapply(Kenya13to18,mode) # Check resulting data head(Kenya13to18,5) # Remove rows with incomplete data Kenya13to18 <- drop_na(Kenya13to18) # Check resulting data head(Kenya13to18,5) # Rename a misnamed cell Kenya13to18$Geo1 <- gsub("01 National","1 National", Kenya13to18$Geo1) Kenya13to18$Geo1 <- gsub("02 Counties","2 Counties", Kenya13to18$Geo1) # Rename cells Kenya13to18$Geo1 <- gsub("1 National","National", Kenya13to18$Geo1) Kenya13to18$Geo1 <- gsub("2 Counties","Counties", Kenya13to18$Geo1) # Check resulting data head(Kenya13to18,5) # Check data types sapply(Kenya13to18,mode) # Get data summary summary(Kenya13to18) # Summarize data SummaryRegionKenya13to18 <- Kenya13to18 %>% group_by(Year, Geo1) %>% summarize(ExpenditureByRegion = sum(`Final Budget (Approved Estimate)`)) # View resulting data view(SummaryRegionKenya13to18) # Remove row with incomplete data SummaryRegionKenya13to18 <- SummaryRegionKenya13to18[-c(9),] # View resulting data view(SummaryRegionKenya13to18) # plot data SummaryRegionPlotKenya13to18 <- SummaryRegionKenya13to18 %>% ggplot(aes(x = Year, y = ExpenditureByRegion, fill = Geo1)) + geom_bar(stat="identity",position="dodge") + labs(title="Absolute National and Regional Aggregated \n Budget Expenditure Allocations \n in Kenya between 2013 and 2017", x="Year", y="Allocated Expenditure (Kshs)", fill="") + scale_fill_manual(values = c("National" = "red", "Counties" = "black")) + theme(axis.text.x = element_text( size = 12, angle = 45, hjust = 0.5, vjust = 0.5), axis.text.y = element_text( size = 12), text = element_text(size = 16), plot.title = element_text(hjust = 0.5)) ggsave("plots/SummaryRegionPlotKenya13to18.svg", SummaryRegionPlotKenya13to18) # Calculate relative proportions SummaryRegionKenya13to18Percent <- SummaryRegionKenya13to18 %>% group_by(Year) %>% mutate(Percent = (ExpenditureByRegion / sum(ExpenditureByRegion)) * 100 ) %>% ungroup() # view data frame view(SummaryRegionKenya13to18Percent) # plot data SummaryRegionPlotKenya13to18Percent <- SummaryRegionKenya13to18Percent %>% ggplot(aes(x = Year, y = Percent, fill = Geo1)) + geom_bar(stat="identity") + labs(title="Relative National and Regional Aggregated \n Budget Expenditure Allocations \n in Kenya between 2013 and 2017", x="Year", y="Allocated Expenditure (%)", fill="") + scale_fill_manual(values = c("National" = "red", "Counties" = "black")) + theme(axis.text.x = element_text( size = 12, angle = 45, hjust = 0.5, vjust = 0.5), axis.text.y = element_text( size = 12), text = element_text(size = 16), plot.title = element_text(hjust = 0.5)) ggsave("plots/SummaryRegionPlotKenya13to18Percent.svg", SummaryRegionPlotKenya13to18Percent) # Summarize data SummaryPurposeKenya13to18 <- Kenya13to18 %>% group_by(Year, Class) %>% summarize(ExpenditureByPurpose = sum(`Final Budget (Approved Estimate)`)) # View resulting data sapply(SummaryPurposeKenya13to18,mode) view(SummaryPurposeKenya13to18) # remove rows with revenue and BTL Funds and deposits SummaryPurposeKenya13to18 <- subset(SummaryPurposeKenya13to18,Class!="2 Revenue") SummaryPurposeKenya13to18 <- subset(SummaryPurposeKenya13to18,Class!="4 Funds & Deposits (BTL)") # Rename cells SummaryPurposeKenya13to18$Class <- gsub("0 Recurrent Expenditure","Recurrent", SummaryPurposeKenya13to18$Class) SummaryPurposeKenya13to18$Class <- gsub("1 Development Expenditure","Development", SummaryPurposeKenya13to18$Class) levels = rev(SummaryPurposeKenya13to18$Class) # Calculate relative proportions SummaryPurposeKenya13to18Percent <- SummaryPurposeKenya13to18 %>% group_by(Year) %>% mutate(Percent = (ExpenditureByPurpose / sum(ExpenditureByPurpose)) * 100 ) %>% ungroup() # view data frame view(SummaryPurposeKenya13to18Percent) # plot data SummaryPurposePlotKenya13to18 <- SummaryPurposeKenya13to18 %>% ggplot(aes(x = Year, y = ExpenditureByPurpose, fill = Class, order = -ExpenditureByPurpose)) + geom_bar(stat="identity",position="dodge") + labs(title="Absolute Aggregated \n Budget Expenditure Allocations \n in Kenya by Class between 2013 and 2017", x="Year", y="Allocated Expenditure (Kshs)", fill="") + scale_fill_manual(values = c("Recurrent" = "red", "Development" = "black"))+ theme(axis.text.x = element_text( size = 12, angle = 45, hjust = 0.5, vjust = 0.5), axis.text.y = element_text( size = 12), text = element_text(size = 16), plot.title = element_text(hjust = 0.5)) ggsave("plots/SummaryPurposePlotKenya13to18.svg", SummaryPurposePlotKenya13to18) SummaryPurposePlotKenya13to18Percent <- SummaryPurposeKenya13to18Percent %>% ggplot(aes(x = Year, y = Percent, fill = Class, order = -Percent)) + geom_bar(stat="identity") + labs(title="Relative Aggregated Budget \n Expenditure Allocations in Kenya by \n Class between 2013 and 2017", x="Year", y="Allocated Expenditure (% of total)", fill="") + scale_fill_manual(values = c("Recurrent" = "red", "Development" = "black"))+ theme(axis.text.x = element_text( size = 12, angle = 45, hjust = 0.5, vjust = 0.5), axis.text.y = element_text( size = 12), text = element_text(size = 16), plot.title = element_text(hjust = 0.5)) ggsave("plots/SummaryPurposePlotKenya13to18Percent.svg", SummaryPurposePlotKenya13to18Percent)