Reading in Data

crops <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-19/crops.csv')
## 
## -- Column specification --------------------------------------------------------
## cols(
##   SubCounty = col_character(),
##   Farming = col_double(),
##   Tea = col_double(),
##   Coffee = col_double(),
##   Avocado = col_double(),
##   Citrus = col_double(),
##   Mango = col_double(),
##   Coconut = col_double(),
##   Macadamia = col_double(),
##   `Cashew Nut` = col_double(),
##   `Khat (Miraa)` = col_double()
## )

Package with more data

p_load_gh("Shelmith-Kariuki/rKenyaCensus")

county_gps <- rKenyaCensus::CountyGPS %>% 
  mutate(SubCounty = County)

df <- inner_join(crops, county_gps)
## Joining, by = "SubCounty"
df <- df %>% 
  select(-SubCounty) %>% 
  relocate(County, .before = Farming)

df <- df %>% 
  pivot_longer(Farming:`Khat (Miraa)`, names_to = "Crop", values_to = "Value")

df <- df %>% 
  mutate(Value = replace_na(Value, 0))

Plotting

df %>% 
  mutate(Crop = fct_reorder(Crop, Value, .desc = T)) %>% 
  ggplot(aes(Longitude, Latitude, size = Value, colour = Crop)) +
  geom_point() +
  facet_wrap(~Crop)

Shapefiles

shp_files <- rKenyaCensus::KenyaCounties_SHP

rKenyaCensus::V4_T2.24
## # A tibble: 393 x 19
## # Groups:   County [48]
##    County SubCounty AdminArea Farming ExoticCattle_Da~ ExoticCattle_Be~
##    <chr>  <chr>     <chr>       <dbl>            <dbl>            <dbl>
##  1 xxx    KENYA     xxx       6354211          2209980           559174
##  2 MOMBA~ MOMBASA   County      12497             3015             1384
##  3 MOMBA~ CHANGAMWE SubCounty     618               49               39
##  4 MOMBA~ JOMVU     SubCounty    2418              904               86
##  5 MOMBA~ KISAUNI   SubCounty    6257             1034              552
##  6 MOMBA~ LIKONI    SubCounty    1863              527              443
##  7 MOMBA~ MVITA     SubCounty     309              114               81
##  8 MOMBA~ NYALI     SubCounty    1032              387              183
##  9 KWALE  KWALE     County     108074            10811             6117
## 10 KWALE  KINANGO   SubCounty   13855             1036             1286
## # ... with 383 more rows, and 13 more variables: IndigenousCattle <dbl>,
## #   Sheep <dbl>, Goats <dbl>, Camels <dbl>, Donkeys <dbl>, Pigs <dbl>,
## #   IndigenousChicken <dbl>, ExoticChicken_Layers <dbl>,
## #   ExoticChicken_Broilers <dbl>, Beehives <dbl>, Rabbits <dbl>,
## #   FishPonds <dbl>, FishCages <dbl>
religions <- rKenyaCensus::V4_T2.30 %>% as_tibble()

df_2 <- inner_join(county_gps, religions)
## Joining, by = "County"
df_2 <- df_2 %>% 
  pivot_longer(Catholic:NotStated , names_to = "Religion", values_to = "Value")

df_2 <- df_2 %>% 
  mutate(Value = replace_na(Value, 0))

df_2 <- df_2 %>% 
  mutate(Religion_share = Value / Total)

library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
# jpeg(
#     filename="figures/kenya_religions_multiple.jpeg",
#     width=8,
#     height=6,
#     units="in",
#     res=1000)

df_2 %>% 
  filter(Religion != "NotStated") %>% 
  mutate(Religion = str_to_title(Religion)) %>% 
  mutate(Religion = fct_reorder(Religion, Value, .desc = T)) %>% 
  ggplot(aes(Longitude, Latitude, size = Religion_share, colour = Religion)) +
  # geom_point(aes(Longitude, Latitude, size = 2*Religion_share), colour = "black") +
  geom_point() +
  borders(regions = "Kenya") +
  facet_wrap(~ Religion) +
  theme(legend.position = "bottom",
        panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +
  # scale_color_brewer(palette = "Paired") +
  scale_size_continuous(labels = percent) +
  labs(x = "",
       y = "",
       title = "Kenya's religions",
       subtitle = "By county",
       size = "Religion's share of county population",
       caption = "Data: Shelmith Kariuki via TidyTuesday\nGraphic: Jonathan Jayes") +
  guides(colour = "none")

# dev.off()

Decision boundary prep

# most prevalent religion
df_3 <- df_2 %>% 
  group_by(County) %>% 
  filter(Religion_share == max(Religion_share)) %>% 
  ungroup() 

# jpeg(
#     filename="figures/kenya_religions_dominant.jpeg",
#     width=8,
#     height=6,
#     units="in",
#     res=1000)

df_3 %>%  
  mutate(Religion = str_to_title(Religion),
         County = str_to_title(County),
         Religion = fct_reorder(Religion, Religion_share, .desc = T)) %>% 
  ggplot(aes(Longitude, Latitude)) +
  geom_point(aes(size = Religion_share, colour = Religion)) +
  geom_text(aes(Longitude, Latitude, label = County), check_overlap = T, vjust = 1) +
  borders(regions = "Kenya") +
  scale_color_brewer(palette = "Paired") +
    theme(legend.position = "bottom",
          legend.box = "vertical",
        panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +
  scale_size_continuous(labels = percent) +
  labs(x = "",
       y = "",
       title = "Dominant religion in each Kenyan county",
       size = "Religion's share of county population",
       caption = "Data: Shelmith Kariuki via TidyTuesday\nGraphic: Jonathan Jayes")

# dev.off()