Tiles

Welcome to #30DayChartChallenge 2022 day 23

Networks
Published

April 23, 2022

Source:

  • https://ourworldindata.org/financing-education
library(tidyverse)
edu_exp <- read_csv("total-government-expenditure-on-education-gdp.csv")
  edu_exp1 <- edu_exp%>%
  janitor::clean_names()%>%
  rename(value=government_expenditure_on_education_total_percent_of_gdp)

edu_exp2 <- complete(edu_exp1, entity,year,fill=list(value=0))
world<- map_data("world")
regions<- plyr::count(world$region)%>%select(-freq)%>%unlist()

edu_exp3 <- edu_exp2%>%
  filter(entity%in%regions)
my_countries <- edu_exp3%>%
  filter(!entity=="Zimbabwe")%>%
  mutate(year2=cut(year,4))%>%
  group_by(entity)%>%
  summarize(tot=sum(value))%>%
  ungroup()%>%
  arrange(-tot)%>%
  slice(1:50)%>%
  unlist()


edu_exp4 <- edu_exp3%>%
  filter(entity%in%my_countries)%>%
  mutate(entity=as.factor(entity),
         year2=cut(year,4))%>%
  group_by(entity,year2)%>%  #2500
  mutate(value2=(value-mean(value))/(sd(value)/sqrt(2500)))%>%
  ungroup()


edu_exp4%>%summary
library(plotly)
library(hrbrthemes)
library(extrafont)
# loadfonts()
edu_exp4%>%count(entity)
set.seed(123)
p <- ggplot(edu_exp4, aes(x=year, y=fct_reorder(entity,value))) +
  geom_tile(aes(fill = value),color="black") +
  geom_vline(aes(xintercept=c(1969.5)),linetype="dashed",size=1,alpha=0.2)+
  geom_vline(aes(xintercept=c(1979.5)),linetype="dashed",size=1,alpha=0.2)+
  geom_vline(aes(xintercept=c(1989.5)),linetype="dashed",size=1,alpha=0.2)+
  geom_vline(aes(xintercept=c(1999.5)),linetype="dashed",size=1,alpha=0.2)+
  geom_vline(aes(xintercept=c(2009.5)),linetype="dashed",size=1,alpha=0.2)+
  geom_vline(aes(xintercept=c(2019.5)),linetype="dashed",size=1,alpha=0.2)+
  scale_fill_gradient(low="white",high="blue",
                      breaks=c(1.5,12),
                      labels=c("Min","Max")) +
  guides(fill = guide_colourbar(barwidth = 9, 
                                barheight = 2,title.position = "top",
                                label = F,
                                ticks = FALSE))+
  scale_x_continuous(expand=c(0,0),
                     breaks=seq(1970,2019,10),
                     label=c("1970-1979","1980-1989","1990-1999","2000-2009","2010-2019"))+
  labs(title = "Government expenditure on education", subtitle="Selected countries with on average total (% of GDP) between 3 and 9%",
       caption="#30DayChartChallenge 2022 #Day23 -Tiles
       DataSource: UNESCO Institute for Statistics via OurWorldInData\n 
       DataViz: Federica Gazzelloni",
       x="",y="",fill="AVG tot GDP %")+
    theme_ipsum()+
  theme(text=element_text(size=12,face="bold",
                          family="Roboto Condensed"),
        axis.text.x = element_text(size=16,vjust=0.5,hjust=-0.5),
        axis.text.y = element_text(size=15),
        plot.title = element_text(size=42,
                                  family="Roboto Condensed"),
        plot.subtitle = element_text(family="Roboto Condensed",size=18,face="bold"),
        plot.caption = element_text(family="Roboto Condensed",size=15,hjust=1,face="bold"),
        plot.title.position = "panel",
        plot.background = element_rect(color="grey80",fill="grey80"),
        panel.background = element_rect(color="grey80",fill="grey80"),
        legend.title = element_text(size=18,vjust=1,color="grey30"),
        legend.text = element_text(size=18,color="grey30"),
        legend.position = c(0,-0.09),
        legend.direction = "horizontal",
        plot.margin = margin(10,10,10,1,unit = "pt"))+
  annotate("text",x=1975,y=-4,label="How to read it:\n- white means 0/empty-value\n- color gradient range on avg between 3 and 9%\n- avg calculated from 1970 to 2019",hjust = 0)+
  coord_cartesian(ylim=c(1,50),clip="off")

p
#ggplotly(p)
ggsave("day23_tiles.png",
       dpi=320,
       width = 12,
       height = 14)