GitLab at IIASA

nuts_converter.R 1.67 KiB
Newer Older
# Nuts Transformation

#'
#'
#'
#'
#'nuts heatmap output
#' @param group.by regional levels (fadn region, NUTS1, NUTS2 and NUTS3)
#' @param fadn.data.info
#' @author Yang
#' @describeIn
#' @export
#' @examples

nuts.heatmap.group <- function(group.by, fadn.data.info){
  fadn.data.info = str_data$info
  #todo: check folder

  #create DIR>plots ----
  dir.create(paste0(CurrentProjectDirectory,"/plot"))
  # create group folder ---
  dir.create(paste0(CurrentProjectDirectory,"/plot/", "fadn_",deparse(substitute(group.by)),"_plots"))

  countries <- unique(fadn.data.info$COUNTRY)

  for (country in countries){
    heatmap_data <- fadn.data.info %>%
      filter(COUNTRY == country) %>%
      count({{group.by}},YEAR) %>%
      arrange(YEAR) %>%
      # pivot_wider(names_from = YEAR,values_from=n) %>%
      mutate(across(3:last_col(),function(x)ifelse(is.na(x),0,1)))
    # pivot_longer(c(`2004`:`2018` ), names_to = "YEAR", values_to = "n")

    heatmap_data <- data.frame(lapply(heatmap_data,as.character))

    if (NROW(heatmap_data$NUTS3 %>% unique()) >100 )  {
      text.size = 3
    } else{text.size = 11}


    p <- heatmap_data %>% ggplot(aes(YEAR, {{group.by}}, fill= n)) + geom_tile() +
      theme(legend.position="none") +
      ggtitle(country)+ theme(axis.text.y = element_text(size = text.size))
    #
    # p_name <- country
    #
    # assign(p_name, heatmap_data %>% ggplot ( aes(YEAR,NUTS2, fill= n)) + geom_tile() +
    #          theme(legend.position="none") +
    #          ggtitle(country))
    ggsave(plot = p ,
           filename = paste0(CurrentProjectDirectory,"/plot/","fadn_",deparse(substitute(group.by)),"_plots/",country,".png"),
           width = 18, height = 8)

  }

}