Advertisement
Guest User

#TidyTuesday 2021 W46

a guest
Nov 9th, 2021
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.67 KB | None | 0 0
  1. #TidyTuesday 2021 W46 Africa
  2.  
  3. #libraries ----
  4. library(sf)
  5. library(ggspatial)
  6. library(showtext)
  7. library(patchwork)
  8. library(tidyverse)
  9.  
  10.  
  11. #data ----
  12. library(afrilearndata)
  13. library(afrihealthsites)
  14.  
  15.  
  16. #font ----
  17. font_add_google("Benchnine", "bench")
  18. font_add_google("Lexend Zetta", "lexend")
  19.  
  20.  
  21. #colour ----
  22. dark_colour <- "black"
  23. light_colour <- "white"
  24.  
  25.  
  26.  
  27. #create continent shapes ----
  28. shape_1 <- africontinent %>%
  29.   st_cast("POLYGON") %>%
  30.   slice(1) %>%
  31.   st_simplify(., dTolerance = 0.25)
  32.  
  33.  
  34. shape_2 <- africontinent %>%
  35.   st_cast("POLYGON") %>%
  36.   slice(1) %>%
  37.   st_simplify(., dTolerance = 2)
  38.  
  39.  
  40. #country shapes ----
  41. countries <- africountries %>%
  42.   filter(name %in% c("Dem. Rep. Congo", "Cameroon", "Nigeria")) %>%
  43.   st_cast("POLYGON") %>%
  44.   st_simplify(., dTolerance = 2)
  45.  
  46.  
  47.  
  48.  
  49. #panel 1: DR Congo ----
  50. shape_congo <- countries %>% filter(name == "Dem. Rep. Congo")
  51. health_congo <- afrihealthsites("Democratic Republic of the Congo", plot = "sf")
  52.  
  53.  
  54. p1 <- ggplot() +
  55.   layer_spatial(shape_congo,
  56.                 colour = NA,
  57.                 fill = light_colour) +
  58.   annotation_spatial(health_congo,
  59.                      shape = 15,
  60.                      size = 1,
  61.                      colour = dark_colour) +
  62.   theme_void() +
  63.   theme(plot.background = element_rect(colour = dark_colour, fill = dark_colour))
  64.  
  65.  
  66. #panel 2: Cameroon ----
  67. shape_cam <- countries %>% filter(name == "Cameroon")
  68. health_cam <- afrihealthsites("Cameroon", plot = "sf")
  69.  
  70.  
  71. p2 <- ggplot() +
  72.   layer_spatial(shape_cam,
  73.                 colour = NA,
  74.                 fill = light_colour) +
  75.   annotation_spatial(health_cam,
  76.                      shape = 15,
  77.                      size = 1,
  78.                      colour = dark_colour) +
  79.   theme_void() +
  80.   theme(plot.background = element_rect(colour = dark_colour, fill = dark_colour))
  81.  
  82.  
  83. #panel 3: Nigeria ----
  84. shape_nigeria <- countries %>% filter(name == "Nigeria")
  85. health_nigeria <- afrihealthsites("Nigeria", plot = "sf")
  86.  
  87.  
  88. p3 <- ggplot() +
  89.   layer_spatial(shape_nigeria,
  90.                 colour = NA,
  91.                 fill = light_colour) +
  92.   annotation_spatial(health_nigeria,
  93.                      shape = 15,
  94.                      size = 1,
  95.                      colour = dark_colour) +
  96.   theme_void() +
  97.   theme(plot.background = element_rect(colour = dark_colour, fill = dark_colour))
  98.  
  99.  
  100. #panel 4 ----
  101. p4 <- ggplot() +
  102.   layer_spatial(countries,
  103.                 fill = dark_colour,
  104.                 colour = NA,
  105.                 show.legend = FALSE) +
  106.   layer_spatial(shape_1,
  107.                 size = 3,
  108.                 fill = NA,
  109.                 colour = dark_colour) +
  110.   layer_spatial(shape_2,
  111.                 size = 3,
  112.                 fill = NA,
  113.                 colour = dark_colour) +
  114.   theme_void() +
  115.   theme(plot.background = element_rect(colour = light_colour, fill = light_colour))
  116.  
  117.  
  118.  
  119.  
  120. #frame ----
  121. frame <- ggplot() +
  122.   geom_polygon(aes(x = c(0, 0, 100, 100),
  123.                    y = c(0, 160, 160, 0)),
  124.                colour = light_colour,
  125.                fill = light_colour) +
  126.   coord_fixed(expand = FALSE) +
  127.   theme_void()
  128.  
  129.  
  130. #annotations ----
  131. showtext_auto()
  132. showtext_opts(dpi = 300)
  133.  
  134.  
  135. a1 <- ggplot() +
  136.   annotate("rect", xmin = 5, ymin = 82, xmax = 49, ymax = 155,
  137.            fill = dark_colour,
  138.            colour = NA) +
  139.   annotate("rect", xmin = 51, ymin = 82, xmax = 95, ymax = 155,
  140.            fill = dark_colour,
  141.            colour = NA) +
  142.   annotate("rect", xmin = 5, ymin = 5, xmax = 49, ymax = 78,
  143.            fill = light_colour,
  144.            colour = NA) +
  145.   annotate("rect", xmin = 51, ymin = 5, xmax = 95, ymax = 78,
  146.            fill = dark_colour,
  147.            colour = NA) +
  148.   scale_x_continuous(limits = c(0, 100)) +
  149.   scale_y_continuous(limits = c(0, 160)) +
  150.   coord_cartesian(expand = FALSE) +
  151.   theme_void()
  152.  
  153.  
  154. a2 <- ggplot() +
  155.   annotate("text", x = 10, y = 73, label = "AFRICA\nRESPONDS",
  156.            hjust = 0, vjust = 1,
  157.            lineheight = 0.9,
  158.            colour = dark_colour,
  159.            family = "lexend",
  160.            fontface = "bold",
  161.            size = 4) +
  162.   annotate("text", x = 10, y = 62, label = "Accurate healthcare location data is essential for a fast,\ncoordinated response to disease outbreaks.",
  163.            hjust = 0, vjust = 1,
  164.            colour = dark_colour,
  165.            family = "bench",
  166.            size = 2) +
  167.   annotate("text", x = 10, y = 150, label = "NIGERIA",
  168.            hjust = 0, vjust = 1,
  169.            colour = light_colour,
  170.            family = "lexend",
  171.            size = 3) +
  172.   annotate("text", x = 10, y = 143, label = "LASSA FEVER\n3,660 Health Sites",
  173.            hjust = 0, vjust = 1,
  174.            colour = light_colour,
  175.            family = "bench",
  176.            size = 4) +
  177.   annotate("text", x = 55, y = 150, label = "CAMEROON",
  178.            hjust = 0, vjust = 1,
  179.            colour = light_colour,
  180.            family = "lexend",
  181.            size = 3) +
  182.   annotate("text", x = 55, y = 143, label = "CHOLERA\n1,761 Health Sites",
  183.            hjust = 0, vjust = 1,
  184.            colour = light_colour,
  185.            family = "bench",
  186.            size = 4) +
  187.   annotate("text", x = 55, y = 73, label = "DEMOCRATIC\nREPUBLIC\nOF THE\nCONGO",
  188.            hjust = 0, vjust = 1,
  189.            colour = light_colour,
  190.            family = "lexend",
  191.            size = 3) +
  192.   annotate("text", x = 55, y = 20, label = "EBOLA VIRUS DISEASE\n2,739 Health Sites",
  193.            hjust = 0, vjust = 1,
  194.            colour = light_colour,
  195.            family = "bench",
  196.            size = 4) +
  197.   annotate("rect", xmin = 5.5, ymin = 5.5, xmax = 48.5, ymax = 77.5,
  198.            size = 1.5,
  199.            fill = NA,
  200.            colour = dark_colour) +
  201.   annotate("text", x = 5, y = 2.5, label = "Source: WHO Weekly Bulletin (1-7 Nov 2021), afrimapr  |  #TidyTuesday 2021 W46  |   |   #30DayMapChallenge D9   |  @quite_grey",
  202.            hjust = 0,
  203.            colour = dark_colour,
  204.            family = "bench",
  205.            size = 2) +
  206.   scale_x_continuous(limits = c(0, 100)) +
  207.   scale_y_continuous(limits = c(0, 160)) +
  208.   coord_cartesian(expand = FALSE) +
  209.   theme_void()
  210.  
  211.  
  212.  
  213. #compile ----
  214. frame +
  215.   inset_element(a1, 0, 0, 1, 1, align_to = "plot") +
  216.   inset_element(p3, 5/100, 60/160, 45/100, 155/160,
  217.                 align_to = "plot") +
  218.   inset_element(p2, 55/100, 70/160, 90/100, 155/160,
  219.                 align_to = "plot") +
  220.   inset_element(p1, 58/100, 20/160, 90/100, 55/160,
  221.                 align_to = "plot") +
  222.   inset_element(p4, 6/100, 5/160, 47/100, 60/160,
  223.                 align_to = "plot") +
  224.   inset_element(a2, 0, 0, 1, 1, align_to = "plot")
  225.  
  226.  
  227. #save ----
  228. ggsave("./output/plot/africa_map_bw.png", height = 16, width = 10, unit = "cm")
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement