This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclimate_map.R
59 lines (55 loc) · 2.31 KB
/
climate_map.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
#'Map climate data
#'@description Create maps of climate data. It requires two data inputs, a map dataframe, and a climate dataframe. The climate data must have one data point per spatial mapping point,e.g. 1 data point per country or basin being mapped.
#'
#'@param map_df a map dataframe generated from create_map_df()
#'@param data_df a climate dataframe with one piece of data to be mapped to each unique spatial polygon.
#'@param return_map True returns a ggplot2 object, False returns a dataframe where data items are matched to their polygon that you can plot later on.
#'@return Either a ggplot2 map or a dataframe depending on the parameter return_map
#'@examples \dontrun{
#' #Set the kmlpath option
#' options(kmlpath = "~/kmltemp2")
#' ##Here we use a list basins for Africa
#' af_basin <- create_map_df(Africa_basin)
#' af_basin_dat <- get_ensemble_temp(Africa_basin,"annualanom",2080,2100)
#' ## Subset data to just one scenario, and one percentile
#' af_basin_dat <- subset(af_basin_dat,af_basin_dat$scenario == "a2")
#' af_basin_dat <- subset(af_basin_dat,af_basin_dat$percentile == 50)
#' af_map <- climate_map(map_df = af_basin, data_df = af_basin_dat, return_map = TRUE)
#' af_map + scale_fill_continuous("Temperature \n anomaly",low="yellow",high = "red") + theme_bw()
#'
#'}
#'
#'@export
climate_map <- function(map_df,
data_df,
return_map = TRUE){
### You can't plot more that one piece of data on a map
### so we need to check that there's not more data than regions to plot
if(length(unique(map_df$ID)) != dim(data_df)[1]){
stop("You can't have more than one piece fo data for each region to map")
}
## Order data for easy matching
data_df <- data_df[order(data_df$locator),]
map_df <- map_df[order(map_df$ID),]
ids <- unique(map_df$ID)
data_vec <- vector()
for(i in 1:length(ids)){
data_vec <- c(data_vec,rep(data_df$data[i],sum(map_df$ID==ids[i])))
}
if(is.list(data_vec)){
data_vec <- unlist(data_vec)
}
map_df$data <- data_vec
if(length(unique(map_df$data)) == 1) {
map <- ggplot(map_df, aes(x = long, y = lat, group = group)) +
geom_polygon()
} else {
map <- ggplot(map_df, aes(x = long, y = lat, group = group, fill = data)) +
geom_polygon()
}
if (return_map) {
return(map)
} else {
return(map_df)
}
}