knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
# knitr::opts_knit$get("root.dir")  # alternative to the previous line
# the default autosave location will depend on this being setup
options(warn=-1)
knitr::opts_chunk$set(echo = TRUE)
library(FPEMlocal)
devtools::load_all()
fit1 <- fit_fp_c(
  division_numeric_code = 4,
  is_in_union = "Y",
  first_year = 1970,
  last_year = 2030
)

fit2 <- fit_fp_c(
  surveydata_filepath = "data-raw/afghanistan_4_married_example.csv",
  division_numeric_code = 4,
  is_in_union = "Y",
  first_year = 1970,
  last_year = 2030,
  subnational = TRUE
)
results1 <- calc_fp_c(fit1)

population_data <- read.csv("data-raw/afghanistan_4_married_popdata_example.csv")
results2 <- calc_fp_c(fit = fit2,
                     population_data = population_data)
results1 <- results1$Y
results2 <- results2$Y
fit <- fit1$Y
indicator <- "contraceptive_use_modern"
observations <- fit$core_data$observations
first_year <- fit$core_data$year_sequence_list$result_seq_years %>% min()
last_year <- fit$core_data$year_sequence_list$result_seq_years %>% max()
y_label = "Proportion"
breaks = seq(
  first_year,
  last_year,
  by = 5
)
results_j <- results[[indicator]]
results_j2 <- results2[[indicator]]
data2 <- tidyr::spread(results_j2, key = percentile, value = value)
plotobj <- tidyr::spread(results_j, key = percentile, value = value) %>%
  ggplot2::ggplot(ggplot2::aes(x = year)) +
  ggplot2::ggtitle(indicator) +
  ggplot2::scale_x_continuous(name = "Year", breaks = breaks) +
  ggplot2::ylab(y_label) +
  ggplot2::theme_bw() +
  ggplot2::theme(
    plot.title = ggplot2::element_text(hjust = 0.5),
    axis.text.x = ggplot2::element_text(angle = 90, hjust = 1)
  ) +
  ggplot2::geom_ribbon(ggplot2::aes(ymin = `2.5%`, ymax = `97.5%`), fill = "blue", alpha = .15) +
  # ggplot2::geom_ribbon(ggplot2::aes(ymin = `10%`, ymax = `90%`), fill = "blue", alpha = .05) +
  ggplot2::geom_line(ggplot2::aes(y = `50%`), color = "blue", alpha = .5, size = 1.3)

plotobj <- plotobj +
  ggplot2::geom_ribbon(ggplot2::aes(ymin = `2.5%`, ymax = `97.5%`), data = data2, fill = "red", alpha = .15) +
  # ggplot2::geom_ribbon(ggplot2::aes(ymin = `10%`, ymax = `90%`), fill = "red", alpha = .05) +
  ggplot2::geom_line(ggplot2::aes(y = `50%`), color = "red", linetype = 2, alpha = .5, size = 1.3)
plotobj
plotobj <- tidyr::spread(results_j, key = percentile, value = value) %>%
  ggplot2::ggplot(ggplot2::aes(x = year)) +
  ggplot2::ggtitle(indicator) +
  ggplot2::scale_x_continuous(name = "Year", breaks = breaks) +
  ggplot2::ylab(y_label) +
  ggplot2::theme_bw() +
  ggplot2::theme(
    plot.title = ggplot2::element_text(hjust = 0.5),
    axis.text.x = ggplot2::element_text(angle = 90, hjust = 1)
  ) +
  ggplot2::geom_ribbon(ggplot2::aes(ymin = `2.5%`, ymax = `97.5%`), fill = "blue", alpha = .15) +
  ggplot2::geom_line(ggplot2::aes(y = `50%`), color = "blue", alpha = .5, size = 1.3)

plotobj <- plotobj +
  ggplot2::geom_line(ggplot2::aes(y = `2.5%`), data = data2, fill = "black", linetype = 2, alpha = .5, size = 1) +
  ggplot2::geom_line(ggplot2::aes(y = `97.5%`), data = data2, fill = "black", linetype = 2, alpha = .5, size = 1) +
  ggplot2::geom_line(ggplot2::aes(y = `50%`), color = "black", linetype = 2, alpha = .5, size = 1.3)
plotobj
#
# observations$subpopulation_labels <- fpem_get_subpopulation_labels(observations)
# plotobj <- plotobj + ggplot2::geom_point(
#   data = observations,
#   ggplot2::aes_string(
#     x = "ref_date",
#     y = indicator,
#     color = "data_series_type",
#     shape = "group_type_relative_to_baseline"
#   ),
#   size = 2) +
#   ggplot2::geom_text(
#     data = observations,
#     ggplot2::aes_string(
#       x = "ref_date",
#       y = indicator,
#       label = "subpopulation_labels"
#     ),
#     size = 3,
#     hjust = -0.3,
#     vjust = -0.3
#   ) +
#   ggplot2::labs(color = "Data series/type", shape = "Group")


FPRgroup/FPEMcountry documentation built on April 24, 2023, 4:32 p.m.