-
Notifications
You must be signed in to change notification settings - Fork 4
/
LUC_plot.R
64 lines (56 loc) · 2.24 KB
/
LUC_plot.R
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
#' Plot function to visualize downscale results
#'
#'
#' @param res Result from downscale
#' @param rasterfile RasterLayer object
#' @param year Specify dates of results to plot (has to be in res)
#' @param LU Specify land-use of results to plot (has to be in res)
#' @param color Pixel color on the map
#' @param label Label of plot legend
#'
#' @return A list containing
#' * \code{LUC.plot} A ggplot object
#' * \code{plot.df} A dataframe used for constructing LUC.plot
#'
#' @export LUC_plot
#'
#' @import ggplot2
#' @import dplyr
#' @import ggthemes
#' @import methods
#'
#' @examples
#' ## A basic example
LUC_plot <- function(res, rasterfile, year=NULL, LU=NULL, color = "Greens", label = "Area in ha per pixel"){
plot_df <- terra::as.data.frame(rasterfile, xy = TRUE, na.rm = FALSE)
to.plot <- res$out.res
colnames(plot_df) <- c("x", "y", "ns")
ns = lu.to = times = value = x = y= NULL
if(is.null(year) & is.null(LU)){
inputs <- to.plot %>% dplyr::group_by(ns, lu.to, times) %>% dplyr::summarise(value = sum(value),.groups = "keep")
} else if(!(is.null(year) | is.null(LU))){
inputs <- to.plot %>% dplyr::group_by(ns, lu.to, times) %>% dplyr::summarise(value = sum(value),.groups = "keep") %>% subset(lu.to==LU & times==year)
} else if(is.null(year)){
inputs <- to.plot %>% dplyr::group_by(ns, lu.to, times) %>% dplyr::summarise(value = sum(value),.groups = "keep") %>% subset(lu.to==LU)
} else {
inputs <- to.plot %>% dplyr::group_by(ns, lu.to, times) %>% dplyr::summarise(value = sum(value),.groups = "keep") %>% subset(times==year)
}
plot_df <- merge(plot_df, inputs, by="ns")
plot_obj <- ggplot2::ggplot() +
ggplot2::geom_tile(data=plot_df, aes(x=x, y=y, fill=value, group=lu.to), alpha=0.8) +
ggplot2::scale_fill_distiller(palette = color , name=label, direction = 1)+
ggplot2::coord_equal() +
ggthemes::theme_map() +
ggplot2::theme(legend.position="bottom") +
ggplot2::theme(legend.key.width=unit(2, "cm"))+
if(is.null(year) & is.null(LU)){
ggplot2::facet_grid(times~lu.to)
} else if(!(is.null(year) | is.null(LU))){
;
} else if(is.null(year)){
ggplot2::facet_wrap(~times)
} else {
ggplot2::facet_wrap(~lu.to)
}
return(list(LUC.plot=plot_obj, plot.df=plot_df))
}