#' Plots bivariate maps
#'
#' This function plots bivariate maps with their color matrix
#' associated with gradients in the two variables or dimensions.
#'
#' @param bivarmap `[RasterLayer,SpatRaster]` \cr Bivariated classified
#' map, such as one generated by [bivarmap::bivarmap_raster()].
#' @param colmatrix `[matrix]` \cr Matrix of colors to be used in the raster
#' classification and plot, created with [bivarmap::colmatrix()].
#'
#' @return A `ggplot` plot of the bivariate map, with colors following a
#' bivariate color matrix, which is optionally also plotted.
#'
#' @example examples/bivarmap_map_example.R
#'
#' @export
bivarmap_map <- function(bivarmap,
colmatrix,
x_legend_pos = 0,
y_legend_pos = 0,
x_legend_size = .2,
y_legend_size = .2){
# Convert to dataframe for plotting with ggplot
bivarmap_data <- raster::as.data.frame(bivarmap, xy = TRUE)
colnames(bivarmap_data)[3] <- "bivarmap_values"
# Make the map using ggplot
bivarmap_gg <- ggplot2::ggplot(bivarmap_data, ggplot2::aes(x = x, y = y)) +
ggplot2::geom_raster(ggplot2::aes(fill = bivarmap_values)) +
ggplot2::scale_fill_gradientn(colours = colmatrix, na.value = "transparent") +
ggplot2::coord_equal() +
ggplot2::theme_bw() +
ggplot2::theme(text = ggplot2::element_text(size = 10, colour = "black"),
legend.position = "none",
plot.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 12, colour = "black"),
axis.text.y = ggplot2::element_text(angle = 90, hjust = 0.5),
axis.text = ggplot2::element_text(size = 12, colour = "black"),
axis.title = ggplot2::element_text(size = 12, colour = "black")) +
ggplot2::labs(x = "Longitude", y = "Latitude")
map_leg <- cowplot::ggdraw() +
cowplot::draw_plot(bivarmap_gg, 0, 0, 1, 1) +
cowplot::draw_plot(BivLegend, x_legend_pos, y_legend_pos,
x_legend_size, y_legend_size)
map_leg
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.