Skip to content

Instantly share code, notes, and snippets.

@halhen
halhen / pellets.Rmd
Created December 8, 2019 15:27
DataVizRequest -- pellet bags
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(tidyverse)
library(lubridate)
```
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(tidyverse)
library(animation)
# Pasted from various sources and published quickly as part of a twitter discussion; not tested if this copy/paste actually runs.
# Result as https://twitter.com/hnrklndbrg/status/859888911337873409
library(tidyverse)
library(lubridate)
df.recent <- read_csv2('/home/henrik/private/vizyns/data/väder/göteborg-a-20170502.csv', skip=15) %>%
transmute(date=`Representativt dygn`,
min=as.numeric(`Lufttemperatur`),
max=as.numeric(`Lufttemperatur_1`))
@halhen
halhen / codes.csv
Last active February 18, 2024 22:49
The people who keep us company - dataviz
We can make this file beautiful and searchable if this error is corrected: It looks like row 8 should actually have 1 column, instead of 2 in line 7.
code;name
01;Personal Care Activities
0101;Sleeping
0102;Grooming
0103;Health-related self care
0104;Personal Activities
0105;Personal Care Emergencies
0199;Personal care, n.e.c.*
02;Household Activities
0201;Housework
library(htmltab)
library(tidyverse)
library(stringr)
URL <- 'https://en.wikipedia.org/w/index.php?title=Global_Terrorism_Index&oldid=779504863'
df <- htmltab(URL, 5) %>%
transmute(Country = str_trim(Country),
score = as.numeric(Score))
# Data from https://www.kaggle.com/bls/american-time-use-survey
library(tidyverse)
library(tidytext)
# Contains both 1st and 2nd level codes; collisions won't appear though since 1-99 is tier 1 and 100+ is two
df.names <- read_csv2('../data/atus/codes.csv') %>%
mutate(code = as.integer(code))
df.resp <- read_csv('../data/atus/atusresp.csv')
@halhen
halhen / joyplot-atus.R
Last active July 17, 2018 13:20
The daily grind - viz
library(tidyverse)
# data from https://www.kaggle.com/bls/american-time-use-survey
df.resp <- read_csv('../data/atus/atusresp.csv')
df.act <- read_csv('../data/atus/atusact.csv', col_types=cols(tustarttim = col_character(), tustoptime = col_character()))
df.sum <- read_csv('../data/atus/atussum.csv')
df.tmp <- df.act %>%
mutate(activity = case_when(trtier2p == 1301 ~ 'Exercise',
@halhen
halhen / elevation.R
Last active November 4, 2020 16:11
# Download elevation tif from eg http://www.eea.europa.eu/data-and-maps/data/digital-elevation-model-of-europe
# First, convert elevation tif to a space delimited xyz (lng lat elevation) file
# $ gdal_translate -of XYZ elevation3x3.tif /tmp/file.xyz
df <- read_delim('/tmp/file.xyz', delim=' ', col_names=FALSE)
df %>%
mutate(X3 = na_if(X3, 0)) %>%
ggplot(aes(X1, -X2 + 20 * X3/max(X3, na.rm=TRUE), group=X2)) +
geom_line(size=0.05) +
# data from http://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/population-distribution-demography/geostat
# Originally seen at http://spatial.ly/2014/08/population-lines/
# So, this blew up on both Reddit and Twitter. Two bugs fixed (southern Spain was a mess,
# and some countries where missing -- measure twice, submit once, damnit), and two silly superflous lines removed after
# @hadleywickham pointed that out. Also, switched from geom_segment to geom_line.
# The result of the code below can be seen at http://imgur.com/ob8c8ph
library(tidyverse)