Last active
July 18, 2023 14:45
-
-
Save jrosell/f4009569252396fc94ba8d67214bbaae to your computer and use it in GitHub Desktop.
Visualizando la tasa de paro diaria y anual de USA desde 1980 en R. Un ejemplo de uso de los paquetes readr, tibble, magrittr, dplyr, ggplot2, scales y lubridate de tidyverse.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
economics_raw <- read_csv("https://gist.githubusercontent.com/jrosell/cf35e54a70914a3b23946c0a0d3a2cee/raw/47e52b910ecb23c3c7a8c549c9fd277797bd9970/economics_long.csv") | |
print(economics_raw, n = 5) | |
glimpse(economics_raw) | |
economics_raw %>% | |
glimpse(x = .) | |
economics_raw %>% | |
select(date, variable, value) %>% | |
glimpse() | |
economics_raw %>% | |
select(date, variable, value) %>% | |
count(variable, sort = TRUE) | |
economics_raw %>% | |
select(date, variable, value) %>% | |
ggplot(aes(x = date, y = value)) | |
economics_raw %>% | |
select(date, variable, value) %>% | |
ggplot(aes(date, value, colour = variable)) + | |
geom_line() | |
economics_raw %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
glimpse() | |
economics_raw %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
mutate(paro = unemploy / pop) %>% | |
glimpse() | |
economics_raw %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
mutate(paro = unemploy / pop) %>% | |
select(date, paro) %>% | |
glimpse() | |
economics_raw %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
mutate(paro = unemploy / pop) %>% | |
select(date, paro) %>% | |
arrange(date) %>% | |
glimpse() | |
economics_raw %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
mutate(paro = unemploy / pop) %>% | |
select(date, paro) %>% | |
arrange(date) %>% | |
filter(date >= '1980-01-01') %>% | |
glimpse() -> economics_dia | |
ggplot(economics_dia, aes(x = date, y = paro)) | |
ggplot(economics_dia, aes(x = date, y = paro)) + | |
geom_line() | |
ggplot(economics_dia, aes(x = date, y = paro)) + | |
geom_line() + | |
scale_y_continuous(labels = scales::percent_format()) | |
ggplot(economics_dia, aes(x = date, y = paro)) + | |
geom_line() + | |
scale_y_continuous(labels = scales::percent_format()) + | |
labs( | |
title = "Ratio de paro diario desde 1980 en USA", | |
x = "Día", | |
y = "% Ratio de paro" | |
) | |
tasa_paro_diaria_1980 <- function(df) { | |
data <- df %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
mutate(paro = unemploy / pop) %>% | |
select(date, paro) %>% | |
arrange(date) %>% | |
filter(date >= '1980-01-01') | |
p <- ggplot(data, aes(x = date, y = paro)) + | |
geom_line() + | |
scale_y_continuous(labels = scales::percent_format()) + | |
labs( | |
title = "Tasa de paro diaria desde 1980 en USA", | |
x = "Día", | |
y = "% paro" | |
) | |
return(p) | |
} | |
tasa_paro_diaria_1980(economics_raw) | |
economics_raw %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
mutate(paro = unemploy / pop) %>% | |
select(date, paro) %>% | |
arrange(date) %>% | |
filter(date >= '1980-01-01') %>% | |
group_by(anual = lubridate::floor_date(date, unit = "years")) %>% | |
summarize(paro = median(paro)) %>% | |
glimpse() -> economics_anual | |
ggplot(economics_anual, aes(x = anual, y = paro)) + | |
geom_line() + | |
scale_y_continuous(labels = scales::percent_format()) + | |
labs( | |
title = "Ratio de paro anual desde 1980 en USA", | |
x = "Año", | |
y = "% Ratio de paro" | |
) | |
tasa_paro_anual_1980 <- function(df) { | |
data <- df %>% | |
select(date, variable, value) %>% | |
pivot_wider(names_from = variable, values_from = value) %>% | |
mutate(paro = unemploy / pop) %>% | |
select(date, paro) %>% | |
arrange(date) %>% | |
filter(date >= '1980-01-01') %>% | |
group_by(anual = lubridate::floor_date(date, unit = "years")) %>% | |
summarize(paro = median(paro)) | |
p <- ggplot(data, aes(x = anual, y = paro)) + | |
geom_line() + | |
scale_y_continuous(labels = scales::percent_format()) + | |
labs( | |
title = "Ratio de paro anual desde 1980 en USA", | |
x = "Año", | |
y = "% Ratio de paro" | |
) | |
return(p) | |
} | |
tasa_paro_anual_1980(economics_raw) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment