GitLab at IIASA

capri_sankey.R 17.9 KiB
Newer Older
Xinxin Yang's avatar
Xinxin Yang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
# Script: filter different markets( just for cake oil market and sugar, cereals...)

#' function reads balance detailed, split it into "biofuels" and "non-biofuels"
#'
#' @param balance_detailed A data frame.
#' @param p_biofuels boolean.
#' @return links
#' @export
#'
prelinks <- function(balance_detailed, p_biofuels = TRUE){
  # balance_detailed <- balance_detailed_Scenario

  # add variable key, itemName and color from products
  balance_detailed <- left_join(balance_detailed, products[,c(2,3,9)], by = c(".i4" = "key"))

  # rename new variable
  colnames(balance_detailed)[13] <- "Commodities"
  colnames(balance_detailed)[1] <- "key"

  balance_detailed$Commodities <- sub("\\[.*?\\]", "", balance_detailed$Commodities)


  library(magrittr)
  # prod_list_fullname <- balance_detailed %>% select(Commodities) %>% extract2(1)

  # remove character begin with [ and end with ]
  # prod_list_fullname <- gsub("\\[.*?\\]", "", prod_list_fullname)
  # p_biofuels = T

  if (p_biofuels){
    selected_list = c("Rape seed",
                      "Soya seed",
                      "Sunflower seed",
                      "Rape seed oil",
                      "Soya oil",
                      "Soya cake",
                      "Sunflowe seed cake",
                      "Rape seed cake",
                      "Destilled dried grains from bio-ethanol processing",
                      "Pulses",
                      "Bio ethanol")
  }else{
    selected_list = c("Wheat","Grain maize","Barley", "Other cereals","Poultry meat",
                      "Sugar", "Fish and other acquatic products", "Rice milled")
  }

  #
  balance_detailed_ <- balance_detailed %>% as.data.frame()
  balance_detailed_selected <-  filter(balance_detailed_, balance_detailed_[,13] %in% selected_list)

  as_tibble(balance_detailed_selected)

  #rename "Destilled dried grains from bio-ethanol processing" -> "DDGS"
  # balance_detailed_selected <- balance_detailed_selected %>%
  #   mutate(Commodities = ifelse(Commodities!="Destilled dried grains from bio-ethanol processing",Commodities,"DDGS"))


  # filter
  balance_detailed_all <- balance_detailed_selected %>%
    select(-.i1)  %>% # remove region
    select(-.i5)  %>% # remove year: 2030
    select(-interv_ch)  %>%
    group_by(Commodities) %>%
    summarise_at(vars(supply:exports),sum,na.rm=TRUE)

  library(magrittr)
  prod_list_fullname <- balance_detailed_selected %>% select(Commodities) %>% extract2(1)

  #print(prod_list_fullname)
  vars_to_run <- prod_list_fullname


  left_side <- c("supply","imports")

  links_list <- list()
  #nodes_list <- list()

  vars_to_run <- vars_to_run[!vars_to_run %in%
                               c("Soya oil",
                                 "Soya cake",
                                 "Rape seed oil",
                                 "Rape seed cake",
                                 "Sunflower oil",
                                 "Sunflowe seed cake",
                                 "Bio ethanol",
                                 "Destilled dried grains from bio-ethanol processing"
                               )]
  # "Rape seed"      "Sunflower seed" "Soya seed"      "Pulses"
  for (i in vars_to_run){

    data_df <- reshape2::melt(balance_detailed_all)

    data_df_1 <- data_df %>% filter(Commodities == i)
    data_df_1_l <- data_df_1  %>% filter(variable %in% left_side )
    data_df_1_r <- data_df_1  %>% filter(!variable %in% left_side)
    data_df_1_l_agg <- data_df_1_l %>% group_by(Commodities) %>% summarise(value=sum(value))
    data_df_1_l_agg$variable <- paste0(i)


    first <- merge(data_df_1_l,data_df_1_l_agg,by="Commodities",all=TRUE)

    first <- first[,c("variable.x","variable.y","value.x")]
    colnames(first)[3] <- "value"


    second <- merge(data_df_1_l_agg,data_df_1_r,by="Commodities",all=TRUE)
    second <- second[,c("variable.x","variable.y","value.y")]
    colnames(second)[3] <- "value"

    if (p_biofuels){
      if (i != "Pulses"){
        if (i == "Soya seed") {produced  = c("Soya oil","Soya cake")}
        else if (i == "Rape seed") {produced  = c("Rape seed oil","Rape seed cake") }
        else if (i == "Sunflower seed") {produced  = c("Sunflower oil","Sunflowe seed cake") }
        else {print(i);break}

        data_df_proc_help <- balance_detailed_all %>% filter(Commodities %in% produced)
        data_df_proc_help <- reshape2::melt(data_df_proc_help)
        data_df_proc_help <- data_df_proc_help %>% filter(variable== "supply" )
        data_df_proc_help$variable <- paste(data_df_proc_help$Commodities)

        data_df_proc_help$Commodities <- i
        colnames(data_df_proc_help) <- c("variable.x","variable.y","value")

        second <- rbind(second,data_df_proc_help)
        second <- second %>% filter(variable.y!="processing")

        data_df_proc_help <- balance_detailed_all %>% filter(Commodities %in% produced)
        data_df_proc_help <- reshape2::melt(data_df_proc_help)
        data_df_proc_help <- data_df_proc_help %>% filter(variable!="supply")
        colnames(data_df_proc_help) <- c("variable.x","variable.y","value")
        second <- rbind(second,data_df_proc_help)
        second_imports <- second %>% filter(variable.y=="imports")
        second <- second %>% filter(!variable.y=="imports")
        colnames(second_imports) <- c("variable.y","variable.x","value")
        second <- rbind(second,second_imports)
      }

    }

    combined <- rbind(first,second)
    links_list[[i]] <- combined


    links <- rbindlist(links_list)
    links <- unique(links)

  }

  if(p_biofuels){

    # links[grepl("Bio",variable.x) | grepl("Bio",variable.y)]
    setnames(links,old=c("variable.x","variable.y"),new=c("source","target"))
    # links[grepl("Bio",source) | grepl("Bio",target)]

    i <- c("Bio ethanol","Destilled dried grains from bio-ethanol processing")

    data_df_proc_help <- balance_detailed_all %>% filter(Commodities %in% i)
    data_df_proc_help <- reshape2::melt(data_df_proc_help)
    data_df_proc_help <- data_df_proc_help %>% filter(variable=="supply")
    data_df_proc_help$variable <- paste(data_df_proc_help$Commodities)

    data_df_proc_help$Commodities <- "biofuels"
    colnames(data_df_proc_help) <- c("variable.x","variable.y","value")

    data_df <- reshape2::melt(balance_detailed_all)

    data_df_1 <- data_df[data_df$Commodities %in% i,]
    data_df_1_l <- data_df_1[data_df_1$variable %in% left_side,]
    data_df_1_l$variable <- as.character(data_df_1_l$variable)
    data_df_1_l <- data_df_1_l %>% filter(variable!="supply")
    data_df_1_r <- data_df_1[!data_df_1$variable %in% left_side,]
    data_df_1_r$variable <- as.character(data_df_1_r$variable)
    data_df_1_l_agg <- data_df_1_l%>%group_by(Commodities) %>% summarise(value=sum(value))
    data_df_1_l_agg$variable <- paste0(i)
    first <- merge(data_df_1_l,data_df_1_l_agg,by="Commodities",all=TRUE)
    first <- first[,c("variable.x","variable.y","value.x")]
    colnames(first)[3] <- "value"
    second <- merge(data_df_1_l_agg,data_df_1_r,by="Commodities",all=TRUE)
    second <- second[,c("variable.x","variable.y","value.y")]
    colnames(second)[3] <- "value"
    second <- rbind(second,data_df_proc_help)


    second <- second %>% filter(!variable.y %in% c("processing","biofuels"))


    #nodes <- rbind(nodes,data.frame("name"=i))
    combined <- rbind(first,second)
    setnames(combined,old=c("variable.x","variable.y"),new=c("source","target"))

    links <- rbind(links,combined)

  }
  else{
    setnames(links,old=c("variable.x","variable.y"),new=c("source","target"))

  }

  # remove 0 value and <=900
  links[,value:=ifelse(value==0,NA,value)]
  # links[,value:=ifelse(value<=50,NA,value)]
  links<-na.omit(links)

  return(links)
}

#' function calulates sum of links
#'
#' @param balance_detailed_links links.
#' @return sum
#'
linksum <- function(balance_detailed_links){

  # the total value of different products from source
  linkcopysum<- aggregate(balance_detailed_links$value,
                          by=list(Category=balance_detailed_links$source),
                          FUN=sum)

  # add a column: the total value of different prodcuts from target
  linkcopysum <- merge(linkcopysum,
                       aggregate(balance_detailed_links$value,
                                 by=list(Category=balance_detailed_links$target),
                                 FUN=sum),
                       by="Category")

  return(linkcopysum)

}

#'  function Replaces specific characters within strings
#' @param old old column names.
#' @param new new column names.
#' @param df replace data frame
#' @return df
#'
#'
#'
replacestr <- function(old, new, df){
  df$label <- gsub(old, new, df$label)
  return(df)
}

#'  function get links and nodes for drawing a sankey diagram
#' @param baseline Baseline balance market.
#' @param scenario Scenario balance market.
#' @param p_baseline boolean.
#'
#' @return links and nodes
#' @export
#'
#'
links_nodes <- function(baseline, scenario, p_baseline){

  linksum_baseline = linksum(baseline)
  linksum_scenario = linksum(scenario)

  # percentage diferences between scenario and baseline
  linksum_scenario$percent_source = (linksum_scenario$x.x*100/linksum_baseline$x.x)-100
  linksum_scenario$percent_target = (linksum_scenario$x.y*100/linksum_baseline$x.y)-100

  #links = baseline
  if (p_baseline){
    links = baseline
  }else{links= scenario}


  nodes <- data.frame("name" = c(unique(c(as.character(links$source),links$target)) ))

  # product: names -> number
  links$source <- sapply(links$source,function(x)which(nodes$name %in% x))
  links$source  <- links$source -1
  links$target <- sapply(links$target,function(x)which(nodes$name %in% x))
  links$target <- links$target-1

  if (p_baseline){
    links$group <- paste0(gsub(" ", "_", baseline$source),"->", gsub(" ", "_", baseline$target))
  }else{
    links$group <- paste0(gsub(" ", "_", scenario$source),"->", gsub(" ", "_", scenario$target))
  }

  source_baseline <- baseline %>% as.data.frame() %>%
    filter(source %in% c("imports", "supply")) %>%
    group_by(source) %>%
    summarise(sum(value))%>%
    rename(name=source, sum = `sum(value)`)

  source_scenario <- scenario %>% as.data.frame() %>%
    filter(source %in% c("imports", "supply")) %>%
    group_by(source) %>%
    summarise(sum(value))%>%
    rename(name=source, sum = `sum(value)`)

  # calculate percent
  source_scenario$percent<- (source_scenario$sum/source_baseline$sum)*100-100

  target_baseline <- baseline %>% as.data.frame() %>%
    filter(!(target %in% c("imports", "supply"))) %>%
    group_by(target) %>%
    summarise(sum(value)) %>%
    rename(name=target, sum = `sum(value)`)


  target_scenario <- scenario %>% as.data.frame() %>%
    filter(!(target %in% c("imports", "supply"))) %>%
    group_by(target) %>%
    summarise(sum(value)) %>%
    rename(name=target, sum = `sum(value)`)

  # calculate percent
  target_scenario$percent<- (target_scenario$sum/target_baseline$sum)*100-100

  all_baseline <- rbind(source_baseline, target_baseline)
  all_scenario <- rbind(source_scenario, target_scenario)

  # if p_basline != TRUE -> add percent for nodes
  if(!p_baseline){
    # nodes$label <- ifelse(nodes$percent_target=="",
    #                       as.character(nodes$label),
    #                       paste0(nodes$label, " (", nodes$percent_target,"%", ")"))

    all_scenario$label <- paste0(all_scenario$name, ": ",
                                 format(round((all_scenario$sum)/1000,1), decimal.mark = ","),
                                 "K (",round(all_scenario$percent,1), "%)" )
    all <- all_scenario
    #nodes$levels <- 1:3
  }else{
    all_baseline$label <- paste0(all_baseline$name, ": ",
                                 format(round((all_baseline$sum)/1000,1), decimal.mark = "," ), "K" )
    all <- all_baseline }


  nodes_ <- left_join(nodes, all)

  rm(nodes)
  nodes <- nodes_

  nodes$rank=c(1:nrow(nodes))

  nodes = merge(nodes, linksum_scenario[,c("Category","percent_target")],
                by.x="name",
                by.y="Category",
                all.x=TRUE)

  nodes = nodes[order(nodes$rank,decreasing = FALSE),]

  nodes$percent_target = round(nodes$percent_target,0)


  nodes$percent_target <- ifelse(nodes$name=="biofuels","",nodes$percent_target)
  nodes[is.na(nodes)]<-""

  selectedcolor <- bind_rows(products %>% select(c(itemName,color)),
                             dim5s %>% select(c(itemName,color)))

  selectedcolor$itemName <- sub("\\[.*?\\]", "", selectedcolor$itemName)
  nodes <-  left_join(nodes, selectedcolor, by = c("name" = "itemName"))


  #print(nodes)
  nodes <- nodes %>% select(c(rank,name,label,color))


  # add links color

  links_temp <- links
  links_temp$color <- ""
  colorlink <- c("Rape", "Sunflowe", "Soya", "Pulses",
                 "Wheat","Grain","Barley",
                 "Other","Poultry",
                 "Sugar", "Fish",
                 "Rice")


  # get colors from nodes

  color_nodes <- c("Wheat|Grain|Barley|Other|Poultry|Sugar|Fish|Rice|Rape seed$|Sunflowe seed|Soya seed$|Pulses")

  getcolor <-  nodes[grep(color_nodes, nodes$name), ] %>% select(c(name, color))


  library(colorspace)
  for (colorcol in colorlink){
    temp <- getcolor %>%
      filter(grepl(colorcol, getcolor$name))
    message(colorcol)
    print(links_temp[grepl(colorcol, group), ])
    print(links_temp[grep(colorcol, group), ])
    links_temp[grep(colorcol, group), ]$color <- paste0(colorspace::lighten(temp$color, 0.05),"80")

  }
  print(links_temp)


  links <- links_temp %>%
    mutate(color = ifelse(color=="","rgba(123,123,123,0.1)",color))

  # Replace specific characters within strings
  old=c("supply",
        "human_cons",
        "processing",
        "biofuels",
        "feed",
        "imports",
        "exports",
        "Destilled dried grains from bio-ethanol Processing")
  new=c("Production",
        "Human Consumption",
        "Processing",
        "Biofuels",
        "Feed",
        "Imports",
        "Exports",
        "DDG")
  for (i in 1:length(old)){
    nodes <- replacestr(old[i],new[i], nodes)
  }

  return(list(links,nodes))
}
#' sets the position of nodes
#' @param nodes nodes for drawing a sankey diagram.
#' @return nodes with fixed position
#' @export
#'
nodes_position <- function(nodes){
  nodes$level_x = ""
  nodes$level_y = ""


  if( "Pulses" %in% nodes$name){
    # for oil cake
    nodes <- nodes %>%
      mutate(level_x = ifelse(name %in% c("supply", "imports"), 0.01,
                              ifelse(name %in% c("human_cons", "feed", "exports", "processing"),0.8,
                                     ifelse(name %in% c("Rape seed", "Soya seed", "Sunflower seed","Pulses"), 0.2,
                                            ifelse(name %in% c("Rape seed oil", "Soya oil"),0.35,
                                                   ifelse(name %in% c("Rape seed cake", "Soya cake", "Sunflowe seed cake", "biofuels"), 0.55, 0.7))) ))) %>%
      mutate(level_y = ifelse(name %in% c("supply", "imports"), c(0.1,0.2),
                              ifelse(name %in% c("human_cons","Bio ethanol", "feed", "exports", "processing"),c(0.01,0.1,0.25,0.6),
                                     ifelse(name %in% c("Rape seed", "Soya seed","Sunflower seed","Pulses"), c(0.01,0.15,0.3,0.6),
                                            ifelse(name %in% c("Rape seed oil", "Soya oil"),c(0.3,0.6),
                                                   ifelse(name %in% c("Rape seed cake", "Soya cake", "Sunflowe seed cake", "biofuels"), c(0.2,0.3,0.5,0.7), 0.5))) )))


  }else{
    nodes <- nodes %>%
      # for rest
      mutate(level_x = ifelse(name %in% c("supply", "imports"),0.01, ifelse(name %in% c("human_cons","biofuels", "feed", "exports", "processing"),0.8,0.4)))%>%
      mutate(level_y = ifelse(name %in% c("supply", "imports"),c(0.1,0.2), ifelse(name %in% c("human_cons","biofuels", "feed", "exports", "processing"),c(0.1,0.3,0.5,0.7,0.9),c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8))))

  }


  return(nodes)
}

#' function draws sankey diagram and saves html pages for sankey
#'
#'
#' @param links a data frame object with the links between the nodes. It should have include the Source and Target for each link. An optional Value variable can be included to specify how close the nodes are to one another.
#' @param nodes a data frame containing the node id and properties of the nodes. If no ID is specified then the nodes must be in the same order as the Source variable column in the Links data frame. Currently only grouping variable is allowed.
#' @param p_baseline boolean.
#' @param png boolean. if TRUE, the sankey chart will be saved.
#'
#' @return sankey diagram.
#' @export
plot_sankey <- function(links, nodes, p_baseline, Png){

  if(p_baseline){if("Bio ethanol" %in% nodes$name){ NAME <- "baseline"; print("Biofuels")} else{ NAME <- "baseline_Rest"}}
  else{if("Bio ethanol" %in% nodes$name){ NAME <- "scenario"; print("Biofuels")} else{ NAME <- "scenario_Rest"}}

  fig <- plot_ly(
    width = 800,
    height = 800,
    type = "sankey",
    domain = list(
      x =  c(0,1),
      y =  c(0,1)
    ),
    orientation = "h",
    arrangement = 'snap',
    valueformat = ".0f",
    valuesuffix = " tonnes",

    node = list(
      label = nodes$label,
      color = nodes$color,
      x = nodes$level_x,
      y= nodes$level_y,
      pad = 20,
      thickness = 15,
      line = list(
        color = "blau",
        width = 1
      )
    ),

    link = list(
      source = links$source,
      target = links$target,
      value =  links$value,
      color = links$color
      #label =  links$group
    )
  )  %>% layout(
    autosize = F,
    margin = list(l=10, r=10, b=50, t=50, pad=40),
    title = "Market Balance in Tonnens",
    font = list(size = 13, family ="Arial"),
    xaxis = list(showgrid = F, zeroline = F),
    yaxis = list(showgrid = F, zeroline = F)
  )

  #
  library(htmlwidgets)
  print(paste0("Export sankey diagram: ",paste0(outdata.dir,"/",NAME,"_fig.html")))
  saveWidget(fig, paste0(outdata.dir,"/",NAME,"_fig.html"), selfcontained = F, libdir = "lib")
  library(webshot)
  #you convert it as png
  if(png){
  webshot(paste0(outdata.dir,"/",NAME,"_fig.html"),paste0(outdata.dir,"/png/",NAME,"_fig.png"),
  vwidth = 1000, vheight = 600)}
  fig
}