Newer
Older
# Nuts Transformation
#'
#'
#'
#'
#'nuts heatmap output
#' @param group.by regional levels (REGION, NUTS1, NUTS2 or NUTS3)
#' @param fadn.data.info fadn info data
#' @author Yang
#' @describeIn
#' @export
#' @examples
#' ## NOT run:
#' nuts.heatmap.group(NUTS1,str_data$info)
#' ## End (NOT run)
nuts.heatmap.group <- function(group.by, fadn.data.info){
fadn.data.info = str_data$info
#todo: check folder
ifelse(!dir.exists(paste0(CurrentProjectDirectory,"/plot")),
dir.create(paste0(CurrentProjectDirectory,"/plot")), FALSE)
# dir.create(paste0(CurrentProjectDirectory,"/plot"), FALSE)
dir.create(paste0(CurrentProjectDirectory,"/plot/", "fadn_",deparse(substitute(group.by)),"_plots"),
showWarnings = FALSE)
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}
# print(summary(heatmap_data))
text.size = 11
p <- heatmap_data %>% ggplot(aes(YEAR, {{group.by}}, fill= n)) + geom_tile() +
# theme() +
ggtitle(country) + xlab("YEAR") + ylab(deparse(substitute(group.by))) +
theme(axis.text.y = element_text(size = text.size),legend.position="none")
#
# 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)
path_png <- paste0(CurrentProjectDirectory,"/plot/","fadn_",deparse(substitute(group.by)),"_plots/")
png(paste0(path_png,country,".png"))
print(p)
dev.off()