Skip to content

Instantly share code, notes, and snippets.

@halhen
Created December 8, 2019 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halhen/5d23c70dd17e851dc54acd91b6eae97d to your computer and use it in GitHub Desktop.
Save halhen/5d23c70dd17e851dc54acd91b6eae97d to your computer and use it in GitHub Desktop.
DataVizRequest -- pellet bags
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(tidyverse)
library(lubridate)
```
```{r}
df <- jsonlite::fromJSON('https://pastebin.com/raw/DHtDJeMW') %>%
as.data.frame() %>%
jsonlite::flatten() %>%
gather(key, value) %>%
mutate(date = as.Date(as.POSIXct(as.integer(str_replace_all(key, '[^0-9]+', '')), origin = '1970-01-01')))
```
```{r}
df %>%
arrange(date) %>%
mutate(cum.value = cumsum(value)) %>%
ggplot(aes(date, cum.value)) +
geom_line() +
labs(x = '', y = 'Cumulative bags')
ggsave('/tmp/out.png', width=8, height = 5)
```
```{r}
Sys.setlocale("LC_TIME", "en_US.UTF-8")
df %>%
group_by(wday = lubridate::wday(date, label=TRUE, abbr=FALSE)) %>%
summarize(value = sum(value)) %>%
ggplot(aes(fct_rev(wday), value)) +
geom_col() +
coord_flip() +
labs(y = 'Bags', x = '')
ggsave('/tmp/out2.png', width=8, height = 5)
```
```{r}
df %>%
complete(date = seq.Date(min(date), max(date), by = '1 day'), fill = list(value = 0)) %>%
ggplot(aes(date, value)) +
geom_point() +
geom_smooth(se = FALSE, span = 0.4) +
labs(x = '', y = 'Bags per day')
ggsave('/tmp/out3.png', width=8, height = 5)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment