# 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 Kenya06to13 <- read_tsv("data/kenya-boost-2006-2013-reduce.tsv", skip_empty_rows = TRUE, trim_ws = TRUE, na = c("-","NA")) # View part of the data frame since it is large view(head(Kenya06to13,100)) names(Kenya06to13) # Remove rows with incomplete data Kenya06to13 <- drop_na(Kenya06to13) # Check resulting data head(Kenya06to13,5) # Get data types sapply(Kenya06to13,mode) # Get data summary summary(Kenya06to13) # Summarize data according to controller SummaryRegionKenya06to13Estimates <- Kenya06to13 %>% group_by(Year, Adm1) %>% summarize(ExpenditureByRegionEstimates = sum(`Estimates`)) SummaryRegionKenya06to13Executed <- Kenya06to13 %>% group_by(Year, Adm1) %>% summarize(ExpenditureByRegionExecuted = sum(`Executed`)) # Calculate relative proportions SummaryRegionKenya06to13EstimatesPercent <- SummaryRegionKenya06to13Estimates %>% group_by(Year) %>% mutate(Percent = (ExpenditureByRegionEstimates / sum(ExpenditureByRegionEstimates)) * 100 ) %>% ungroup() SummaryRegionKenya06to13ExecutedPercent <- SummaryRegionKenya06to13Executed %>% group_by(Year) %>% mutate(Percent = (ExpenditureByRegionExecuted / sum(ExpenditureByRegionExecuted)) * 100 ) %>% ungroup() # View resulting data view(SummaryRegionKenya06to13Estimates) view(SummaryRegionKenya06to13Executed) view(SummaryRegionKenya06to13EstimatesPercent) view(SummaryRegionKenya06to13ExecutedPercent) # plot data SummaryRegionPlotKenya06to13Estimates <- SummaryRegionKenya06to13Estimates %>% ggplot(aes(x = Year, y = ExpenditureByRegionEstimates, fill = Adm1, order = -ExpenditureByRegionEstimates)) + geom_bar(stat="identity", position=position_dodge2(reverse=TRUE)) + labs(title="Absolute National and Regional Aggregated \n Budget Expenditure Allocations in Kenya \n between 2005 and 2012", x="Year", y="Allocated Expenditure (Kshs)", fill="") + scale_fill_manual(values = c("Central" = "red", "Constituency Fund" = "black", "Social security" = "yellow", "Local Authorities" = "purple"))+ 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/SummaryRegionPlotKenya06to13Estimates.svg", SummaryRegionPlotKenya06to13Estimates) SummaryRegionPlotKenya06to13Executed <- SummaryRegionKenya06to13Executed %>% ggplot(aes(x = Year, y = ExpenditureByRegionExecuted, fill = Adm1)) + geom_bar(stat="identity",position=position_dodge2(reverse=TRUE)) + labs(title="Absolute National and Regional Aggregated \n Budget Expenditure Executions in Kenya \n between 2005 and 2010", x="Year", y="Executed Expenditure (Kshs)", fill="") + scale_fill_manual(values = c("Central" = "red", "Constituency Fund" = "black", "Social security" = "yellow", "Local Authorities" = "purple"))+ 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/SummaryRegionPlotKenya06to13Executed.svg", SummaryRegionPlotKenya06to13Executed) SummaryRegionPlotKenya06to13EstimatesPercent <- SummaryRegionKenya06to13EstimatesPercent %>% ggplot(aes(x = Year, y = Percent, fill = Adm1, order = -Percent)) + geom_bar(stat="identity",position=position_stack(reverse=TRUE)) + labs(title="Relative National and Regional Aggregated \n Budget Expenditure Allocations in Kenya \n between 2005 and 2012", x="Year", y="Allocated Expenditure (%)", fill="") + scale_fill_manual(values = c("Central" = "red", "Constituency Fund" = "black", "Social security" = "yellow", "Local Authorities" = "purple"))+ 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/SummaryRegionPlotKenya06to13EstimatesPercent.svg", SummaryRegionPlotKenya06to13EstimatesPercent) SummaryRegionPlotKenya06to13ExecutedPercent <- SummaryRegionKenya06to13ExecutedPercent %>% ggplot(aes(x = Year, y = Percent, fill = Adm1, order = Percent)) + geom_bar(stat="identity",position=position_stack(reverse=TRUE)) + labs(title="Relative National and Regional Aggregated \n Budget Expenditure Executions in Kenya \n between 2005 and 2010", x="Year", y="Executed Expenditure (%)", fill="") + scale_fill_manual(values = c("Central" = "red", "Constituency Fund" = "black", "Social security" = "yellow", "Local Authorities" = "purple"))+ 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/SummaryRegionPlotKenya06to13ExecutedPercent.svg", SummaryRegionPlotKenya06to13ExecutedPercent) # Summarize data according to purpose SummaryPurposeKenya06to13Estimates <- Kenya06to13 %>% group_by(Year, Class) %>% summarize(ExpenditureByPurposeEstimates = sum(`Estimates`)) SummaryPurposeKenya06to13Executed <- Kenya06to13 %>% group_by(Year, Class) %>% summarize(ExpenditureByPurposeExecuted = sum(`Executed`)) # Calculate relative proportions SummaryPurposeKenya06to13EstimatesPercent <- SummaryPurposeKenya06to13Estimates %>% group_by(Year) %>% mutate(Percent = (ExpenditureByPurposeEstimates / sum(ExpenditureByPurposeEstimates)) * 100 ) %>% ungroup() SummaryPurposeKenya06to13ExecutedPercent <- SummaryPurposeKenya06to13Executed %>% group_by(Year) %>% mutate(Percent = (ExpenditureByPurposeExecuted / sum(ExpenditureByPurposeExecuted)) * 100 ) %>% ungroup() # View resulting data view(SummaryPurposeKenya06to13Estimates) view(SummaryPurposeKenya06to13Executed) view(SummaryPurposeKenya06to13EstimatesPercent) view(SummaryPurposeKenya06to13ExecutedPercent) # plot data SummaryPurposePlotKenya06to13Estimates <- SummaryPurposeKenya06to13Estimates %>% ggplot(aes(x = Year, y = ExpenditureByPurposeEstimates, fill = Class, order = -ExpenditureByPurposeEstimates)) + geom_bar(stat="identity",position=position_dodge2(reverse=FALSE)) + labs(title="Absolute Aggregated Budget Expenditure \n Allocations in Kenya by Class \n between 2005 and 2012", 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/SummaryPurposePlotKenya06to13Estimates.svg", SummaryPurposePlotKenya06to13Estimates) SummaryPurposePlotKenya06to13Executed <- SummaryPurposeKenya06to13Executed %>% ggplot(aes(x = Year, y = ExpenditureByPurposeExecuted, fill = Class, order = -ExpenditureByPurposeExecuted)) + geom_bar(stat="identity",position=position_dodge2(reverse=FALSE)) + labs(title="Absolute Aggregated Budget Expenditure \n Executions in Kenya by Class \n between 2005 and 2010", x="Year", y="Executed 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/SummaryPurposePlotKenya06to13Executed.svg", SummaryPurposePlotKenya06to13Executed) SummaryPurposePlotKenya06to13EstimatesPercent <- SummaryPurposeKenya06to13EstimatesPercent %>% ggplot(aes(x = Year, y = Percent, fill = Class, order = -Percent)) + geom_bar(stat="identity",position=position_stack(reverse=FALSE) ) + labs(title="Relative Aggregated Budget Expenditure \n Allocations in Kenya by Class \n between 2005 and 2012", x="Year", y="Allocated Expenditure (%)", 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/SummaryPurposePlotKenya06to13EstimatesPercent.svg", SummaryPurposePlotKenya06to13EstimatesPercent) SummaryPurposePlotKenya06to13ExecutedPercent <- SummaryPurposeKenya06to13ExecutedPercent %>% ggplot(aes(x = Year, y = Percent, fill = Class, order = -Percent)) + geom_bar(stat="identity",position=position_stack(reverse=FALSE)) + labs(title="Relative Aggregated Budget Expenditure \n Executions in Kenya by Class \n between 2005 and 2010", x="Year", y="Executed Expenditure (%)", 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/SummaryPurposePlotKenya06to13ExecutedPercent.svg", SummaryPurposePlotKenya06to13ExecutedPercent)