/Reading.R

https://github.com/beeb22/makeovermonday · R · 68 lines · 62 code · 6 blank · 0 comment · 2 complexity · eecc84cb1a9caba31921ba0acdb5f7b5 MD5 · raw file

  1. library("httr")
  2. library("readxl")
  3. library(janitor)
  4. library(tidyverse)
  5. library(lubridate)
  6. library(geofacet)
  7. library(ggtext)
  8. GET("https://query.data.world/s/hyolmkmzururu54acof2v5yijykn3i", write_disk(tf <- tempfile(fileext = ".xlsx")))
  9. df <- read_excel(tf, skip = 1) %>%
  10. clean_names()
  11. reading <-
  12. df %>%
  13. separate(year_month,
  14. into = c("year", "month"),
  15. sep = "M") %>%
  16. mutate(
  17. date = str_c(year, month, "01", sep = "-"),
  18. date = ymd(date),
  19. country = factor(country),
  20. country = fct_recode(country,
  21. "EU" = "EU27_2020 - European Union - 27 countries (from 2020)",
  22. "Czech Republic" = "Czechia")
  23. ) %>%
  24. select(category, country, index_2015_100, date)
  25. library(RColorBrewer)
  26. scales::show_col(brewer.pal(2, "Dark2"))
  27. reading %>%
  28. pivot_wider(names_from = category,
  29. values_from = index_2015_100) %>%
  30. clean_names() %>%
  31. mutate(
  32. real_books = books/all_items_hicp,
  33. real_books = real_books - 1
  34. ) %>%
  35. filter(date == as.Date("2015-04-01") | date == as.Date("2020-08-01")) %>%
  36. select(country, real_books, date) %>%
  37. pivot_wider(names_from = date, values_from = real_books) %>%
  38. mutate(change = (`2020-08-01` - `2015-04-01`)*100) %>%
  39. ggplot()+
  40. geom_segment(aes(x = 0, xend = 1, y = `2015-04-01`, yend = `2020-08-01`, colour = change > 0), show.legend = F, size = 1, arrow = arrow(length = unit(0.2, "cm")))+
  41. geom_text(aes(x = 1.2, y = `2020-08-01` - 0.3, label = paste0(round(change, 1), "%"), colour = change > 0),
  42. show.legend = F, family = "IBM Plex Mono Light", size = 4)+
  43. scale_color_manual(values = c("#1B9E77", "#7570B3"))+
  44. labs(
  45. title = "**Have real book prices in Europe <b style = 'color:#7570B3'>increased</b> or <b style = 'color:#1B9E77'>decreased</b> since 2015?**",
  46. subtitle = "There seems to be no consistent pattern across countries",
  47. caption = "data: Eurostat | visualisation: @beeboileau"
  48. )+
  49. theme_void(base_family = "IBM Plex Mono Light",
  50. base_size = 17)+
  51. theme(
  52. plot.title = element_markdown(margin = margin(10,0,10,0), family = "Helvetica"),
  53. plot.subtitle = element_text(margin = margin(0,0,30,0), family = "Helvetica"),
  54. plot.caption = element_text(family = "Helvetica"),
  55. plot.margin = margin(20,20,20,20),
  56. plot.background = element_rect(fill = "#F0EFEB",
  57. color = "#F0EFEB"),
  58. panel.border = element_rect(colour = alpha("black", 0.2), fill = alpha("white", 0))
  59. )+
  60. facet_geo(~country, grid = "eu_grid1", label = "code")+
  61. coord_cartesian(clip = "off", expand = F, xlim = c(-1, 2), ylim = c(-1.1, 1))
  62. ggsave("books.png", height = 10, width = 14)