Git Product home page Git Product logo

obesity's Introduction

Adult obesity in the World

Dominic Royé 02/09/2018

Time serie as heatmap

R Code for the heatmap published at Twitter

Author: Dominic Royé (@dr_xeo) | https://dominicroye.github.io/en/

### libraries ###
library(tidyverse)
library(readxl)
library(viridis)
library(classInt)

## import data ##

data <- read_csv("share-of-adults-defined-as-obese.csv")

## data preparation ##
#change col names
names(data)[3:4] <- c("year","obesity")

#import codes for regions
cod <- read_csv("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv")

#join regions and obesity data
df <- left_join(data,cod,by=c("Code"="alpha-3"))

#filter Europe
df_europe <- df %>% filter(region=="Europe") %>% mutate(date=str_c(str_replace(year,"ID",""),"01","01",sep="-")%>%as.Date())

#countries order by 2016
ord_2016 <- filter(df_europe,year==2016)%>%
              arrange(desc(obesity))%>%
                pull(name)

#breaks for color scale
br <- classIntervals(df_europe$obesity,15,"kmeans")$brks

## plotting ##
#custom theme definitions
theme_heatmap <- theme_minimal()+
                   theme(panel.grid=element_blank(),
                        axis.text.y = element_text(color="white", size=rel(.9)),
                        axis.text.x = element_text(color="white", size=rel(.8)),
                        strip.switch.pad.grid=unit(-0.5,"lines"),
                        strip.text = element_blank(),
                        plot.background = element_rect(fill="gray20"),
                        panel.background = element_rect(fill="gray20"),
                        legend.title=element_blank(),
                        plot.caption=element_text(colour="white"),
                        plot.title = element_text(colour="white"),
                        legend.text = element_text(colour="white", margin=margin(l = 5)),
                        panel.border = element_rect(fill=NA,color="gray20", size=0.5,
                                                    linetype="solid"))

#plot
df_europe %>% mutate(name=factor(name,rev(ord_2016)))%>%
    ggplot(aes(date,name,fill=obesity))+
        geom_tile(colour="grey20")+
          scale_fill_viridis(option="B",na.value="grey20",breaks=br)+
          scale_x_date("",date_breaks = "2 year",date_labels = "%Y",expand=c(0,0))+
            labs(y="",caption="Dominic Royé (@dr_xeo) | Data: ourworldindata.org",
             title="Share of adults defined as obese (%) ordered by 2016\n[18+ years with body-mass index > 30]")+
            guides(fill=guide_colorbar(barheight=22,barwidth = 1))+
        theme_heatmap

Data source

The dataset can be downloaded at ourworldindata.org.

obesity's People

Contributors

dominicroye avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.