diff --git a/.gitignore b/.gitignore index a5d36f2..6236f0e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ inst/doc data-raw/*.csv node_modules docs +data-raw/inputs/ diff --git a/R/data.R b/R/data.R index bf924bf..4b1e973 100644 --- a/R/data.R +++ b/R/data.R @@ -58,3 +58,34 @@ #' } #' @source Wikipedia (\url{https://fr.wikipedia.org/wiki/Climat_de_Paris}) "climate_paris" + + +#' @title eco2mix data +#' +#' @description The dataset contains data about electricity consumption and production in France between 2012 and 2022. +#' +#' @format A data frame with 3,033 observations and 3 variables. +#' +#' @source Rte (Réseau et transport d'électricité) (\url{https://www.rte-france.com/eco2mix/} and \url{https://opendata.reseaux-energies.fr/}) +"eco2mix" + + +#' @title Temperature data +#' +#' @description The dataset contains data about temperatures in France between 2018 and 2022. +#' +#' @format A data frame with 365 observations and 6 variables. +#' +#' @source Enedis (\url{https://data.enedis.fr/explore/dataset/donnees-de-temperature-et-de-pseudo-rayonnement/}) +"temperatures" + + +#' @title Life expectancy data +#' +#' @description The dataset contains data about life expectancy in 1972 and 2007 for 10 countries. +#' +#' @format A data frame with 10 observations and 4 variables. +#' +#' @source gapminder package (\url{https://jennybc.github.io/gapminder/} and \url{https://www.gapminder.org/data/}) +"life_expec" + diff --git a/data-raw/eco2mix.R b/data-raw/eco2mix.R new file mode 100644 index 0000000..db356e2 --- /dev/null +++ b/data-raw/eco2mix.R @@ -0,0 +1,131 @@ +# ------------------------------------------------------------------------ +# +# eCO2mix data +# https://www.rte-france.com/eco2mix +# +# ------------------------------------------------------------------------ + + +# Packages ---------------------------------------------------------------- + +library(data.table) +library(fasttime) + + +complete <- function(data, vars, fill = list()) { + data <- data[do.call(CJ, c( + lapply( + X = mget(vars), + FUN = function(var) { + if (inherits(var, "factor")) { + if (anyNA(var)) { + factor(c(levels(var), NA_character_), levels = levels(var), ordered = is.ordered(var)) + } else { + factor(levels(var), levels = levels(var), ordered = is.ordered(var)) + } + } else { + unique(var) + } + } + ), + list(sorted = FALSE) + )), on = vars] + if (length(fill) > 0 && all(nzchar(names(fill)))) { + for (fillvar in names(fill)) { + data[is.na(get(fillvar)), (fillvar) := fill[[fillvar]]] + } + } + data[] +} + + + +# Download data ----------------------------------------------------------- + +# Source: https://odre.opendatasoft.com/explore/dataset/eco2mix-national-cons-def/ +# and https://odre.opendatasoft.com/explore/dataset/eco2mix-national-tr + + +# Read & transform data --------------------------------------------------- + +# eco2mix <- fread(file = "data-raw/inputs/eco2mix-national-cons-def.csv") +# eco2mix <- eco2mix[, c(5, 6, 9:17)] +# setnames(eco2mix, c("datetime", "consumption", "fuel", "coal", "gas", "nuclear", "wind", "solar", "hydraulic", "pumping", "bioenergies")) + +eco2mix_tr <- fread(file = "data-raw/inputs/eco2mix-national-tr.csv") +eco2mix_tr <- eco2mix_tr[, c(5, 6, 9:17)] +setnames(eco2mix_tr, c("datetime", "consumption", "fuel", "coal", "gas", "nuclear", "wind", "solar", "hydraulic", "pumping", "bioenergies")) + +eco2mix <- copy(eco2mix_tr) +# eco2mix <- rbind(eco2mix, eco2mix_tr) + +eco2mix <- eco2mix[!is.na(consumption)] +eco2mix[, consumption := NULL] +# eco2mix[, date := as.Date(format(datetime, format = "%Y-%m-%d"))] +# eco2mix[, datetime := NULL] +# setcolorder(eco2mix, "date") +eco2mix <- eco2mix[minute(datetime) != 15] +eco2mix <- eco2mix[minute(datetime) != 45] +eco2mix <- eco2mix[datetime >= (max(datetime) - 24*60*60*7)] +eco2mix <- melt( + data = eco2mix, + id.vars = 1, + variable.name = "source", + value.name = "production", + na.rm = TRUE, + variable.factor = FALSE +) +eco2mix <- eco2mix[, list(production = round(mean(production))), by = list(datetime, source)] +eco2mix[, source := factor( + x = source, + levels = c("pumping", "wind", "solar", "nuclear", "hydraulic", "gas", "coal", "fuel", "bioenergies"), + ordered = TRUE +)] +eco2mix <- complete(eco2mix, c("datetime", "source"), list(production = 0)) +setorder(eco2mix, source, datetime) + +eco2mix[] + + +# Use data ---------------------------------------------------------------- + +setDF(eco2mix) +usethis::use_data(eco2mix, internal = FALSE, overwrite = TRUE, compress = "xz") + + + + + +# Test example ------------------------------------------------------------ + +apex(eco2mix[source == "consumption"], aes(date, production), type = "line") + +# data("eco2mix", package = "apexcharter") +apex(eco2mix, aes(datetime, production, fill = source), type = "area") %>% + ax_chart(animations = list(enabled = FALSE), stacked = TRUE) %>% + ax_stroke(width = 1) %>% + ax_fill(opacity = 1, type = "solid") %>% + ax_tooltip(x = list(format = "dd MMM, HH:mm")) %>% + ax_yaxis(labels = list(formatter = format_num("~", suffix = "MW"))) %>% + ax_colors_manual( + list( + "bioenergies" = "#156956", + "fuel" = "#80549f", + "coal" = "#a68832", + "solar" = "#d66b0d", + "gas" = "#f20809", + "wind" = "#72cbb7", + "hydraulic" = "#2672b0", + "nuclear" = "#e4a701", + "pumping" = "#0e4269" + ) + ) %>% + ax_labs( + title = "Electricity generation by sector in France", + subtitle = "Data from \u00e9CO\u2082mix" + ) + + + + + diff --git a/data-raw/life_expectancy.R b/data-raw/life_expectancy.R new file mode 100644 index 0000000..33138d1 --- /dev/null +++ b/data-raw/life_expectancy.R @@ -0,0 +1,65 @@ + + +# Package ----------------------------------------------------------------- + +library(data.table) +library(gapminder) + + + +# Data -------------------------------------------------------------------- + +life_expec <- as.data.table(gapminder::gapminder) +life_expec <- life_expec[year %in% c(1972, 2007), list(country, year, lifeExp)] + +# life_expec <- life_expec[country %in% sample(unique(country), 10)] +life_expec <- life_expec[country %in% c("Botswana", "Ghana", "Iran", "Liberia", "Malaysia", "Mexico", + "Nigeria", "Pakistan", "Philippines", "Zambia")] +life_expec <- dcast(life_expec, country ~ year, value.var = "lifeExp") +life_expec[, type := fifelse(`1972` > `2007`, "decreased", "increased")] + + + +# Use data ---------------------------------------------------------------- + +setDF(life_expec) +usethis::use_data(life_expec, internal = FALSE, overwrite = TRUE, compress = "xz") + + + + + +# Test example ------------------------------------------------------------ + + +pkgload::load_all() + +apex(life_expec, aes(country, x = `1972`, xend = `2007`), type = "dumbbell") %>% + ax_plotOptions( + bar = bar_opts( + dumbbellColors = list(list("#3d85c6", "#fb6003")) + ) + ) %>% + ax_colors("#BABABA") %>% + ax_labs( + title = "Life expectancy : 1972 vs. 2007", + subtitle = "Data from Gapminder dataset", + x = "Life expectancy at birth, in years" + ) + + + +apex(life_expec, aes(country, x = `1972`, xend = `2007`, group = type), type = "dumbbell") %>% + ax_xaxis(type = "category", categories = unique(life_expec$country)) %>% + ax_plotOptions( + bar = bar_opts( + dumbbellColors = list(list("#3d85c6", "#fb6003"), list("#3d85c6", "#fb6003")) + ) + ) %>% + ax_colors(c("#3d85c6", "#fb6003")) %>% + ax_labs( + title = "Life expectancy : 1972 vs. 2007", + subtitle = "Data from Gapminder dataset", + x = "Life expectancy at birth, in years" + ) + diff --git a/data-raw/temperature.R b/data-raw/temperature.R new file mode 100644 index 0000000..37343b3 --- /dev/null +++ b/data-raw/temperature.R @@ -0,0 +1,63 @@ +# ------------------------------------------------------------------------ +# +# temperature data for France +# https://data.enedis.fr/explore/dataset/donnees-de-temperature-et-de-pseudo-rayonnement +# +# ------------------------------------------------------------------------ + + +# Packages ---------------------------------------------------------------- + +library(data.table) +library(fasttime) + + +# Data -------------------------------------------------------------------- + +temperatures <- fread(file = "data-raw/inputs/donnees-de-temperature-et-de-pseudo-rayonnement.csv") +temperatures <- temperatures[, c(6, 7, 8, 2)] +setnames(temperatures, c("year", "month", "day", "temperature")) +temperatures <- temperatures[year > 2017] +temperatures <- temperatures[, list(temperature = round(mean(temperature, na.rm = TRUE), 1)), by = c("year", "month", "day")] +temperatures <- dcast(data = temperatures, formula = month + day ~ year, value.var = "temperature") +temperatures <- temperatures[!(month == 2 & day == 29)] + +temperatures[, low := do.call(pmin, c(as.list(.SD), na.rm = TRUE)), .SDcols = as.character(2018:2021)] +temperatures[, high := do.call(pmax, c(as.list(.SD), na.rm = TRUE)), .SDcols = as.character(2018:2021)] +temperatures[, average := rowMeans(.SD, na.rm = TRUE), .SDcols = as.character(2018:2021)] +temperatures[, (as.character(2018:2021)) := NULL] +# setnames(temperatures, "2022", "temperature") + +temperatures[, date := as.Date("2022-01-01") + (seq_len(.N) - 1)] +temperatures[, (c("month", "day")) := NULL] +setcolorder(temperatures, "date") + +temperatures[] + + +# Save -------------------------------------------------------------------- + +setDF(temperatures) +usethis::use_data(temperatures, internal = FALSE, overwrite = TRUE, compress = "xz") + + + +# Test example ------------------------------------------------------------ + + +pkgload::load_all() + +apex(temperatures, aes(x = date, ymin = low, ymax = high), type = "rangeArea", serie_name = "Low/High (2018-2021)") %>% + add_line(aes(date, `2023`)) %>% + ax_chart(animations = list(enabled = FALSE)) %>% + ax_yaxis(tickAmount = 7, labels = list(formatter = format_num("~", suffix = "°C"))) %>% + ax_colors(c("#8485854D", "#FF0000")) %>% + ax_stroke(width = c(1, 2)) %>% + ax_fill(opacity = 1, type = "solid") %>% + ax_labs( + title = "Temperatures in 2023 with range from 2018 to 2021", + subtitle = "Data from ENEDIS" + ) + + + diff --git a/data/eco2mix.rda b/data/eco2mix.rda new file mode 100644 index 0000000..dc75bc1 Binary files /dev/null and b/data/eco2mix.rda differ diff --git a/data/life_expec.rda b/data/life_expec.rda new file mode 100644 index 0000000..728a897 Binary files /dev/null and b/data/life_expec.rda differ diff --git a/data/temperatures.rda b/data/temperatures.rda new file mode 100644 index 0000000..37c507b Binary files /dev/null and b/data/temperatures.rda differ diff --git a/man/eco2mix.Rd b/man/eco2mix.Rd new file mode 100644 index 0000000..e58b4be --- /dev/null +++ b/man/eco2mix.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{eco2mix} +\alias{eco2mix} +\title{eco2mix data} +\format{ +A data frame with 3,033 observations and 3 variables. +} +\source{ +Rte (Réseau et transport d'électricité) (\url{https://www.rte-france.com/eco2mix/} and \url{https://opendata.reseaux-energies.fr/}) +} +\usage{ +eco2mix +} +\description{ +The dataset contains data about electricity consumption and production in France between 2012 and 2022. +} +\keyword{datasets} diff --git a/man/life_expec.Rd b/man/life_expec.Rd new file mode 100644 index 0000000..7d43ac7 --- /dev/null +++ b/man/life_expec.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{life_expec} +\alias{life_expec} +\title{Life expectancy data} +\format{ +A data frame with 10 observations and 4 variables. +} +\source{ +gapminder package (\url{https://jennybc.github.io/gapminder/} and \url{https://www.gapminder.org/data/}) +} +\usage{ +life_expec +} +\description{ +The dataset contains data about life expectancy in 1972 and 2007 for 10 countries. +} +\keyword{datasets} diff --git a/man/temperatures.Rd b/man/temperatures.Rd new file mode 100644 index 0000000..7030f9f --- /dev/null +++ b/man/temperatures.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{temperatures} +\alias{temperatures} +\title{Temperature data} +\format{ +A data frame with 365 observations and 6 variables. +} +\source{ +Enedis (\url{https://data.enedis.fr/explore/dataset/donnees-de-temperature-et-de-pseudo-rayonnement/}) +} +\usage{ +temperatures +} +\description{ +The dataset contains data about temperatures in France between 2018 and 2022. +} +\keyword{datasets}