Skip to content

Instantly share code, notes, and snippets.

@Pakillo
Last active July 21, 2021 08:15
Show Gist options
  • Save Pakillo/4e951fde802b1c4d3a1b39cc15f6e1c1 to your computer and use it in GitHub Desktop.
Save Pakillo/4e951fde802b1c4d3a1b39cc15f6e1c1 to your computer and use it in GitHub Desktop.
Using {biscale} package to plot bivariate raster maps in R
## Using {biscale} to plot bivariate raster maps

library(ggplot2)
library(biscale)
library(cowplot)
library(raster)
#> Loading required package: sp


## Load rasterbrick (R logo)
rlogo <- brick(system.file("external/rlogo.grd", package="raster"))
plotRGB(rlogo)

plot(rlogo)

## Convert to data frame
rlogodf <- as.data.frame(rlogo, xy = TRUE)


## Create bivariate classes
rlogodf <- bi_class(rlogodf, x = "red", y = "blue")


## Plot

map <- ggplot(rlogodf) +
  geom_raster(aes(x, y, fill = bi_class), show.legend = FALSE) +
  bi_scale_fill(pal = "DkViolet") +
  bi_theme() +
  labs(x = "", y = "")

legend <- bi_legend(pal = "DkViolet",
                    xlab = "More Red",
                    ylab = "More Blue",
                    size = 8)

finalPlot <- ggdraw() +
  draw_plot(map, 0, 0, 1, 1) +
  draw_plot(legend, 0.1, .75, 0.2, 0.2)

finalPlot

Created on 2021-07-21 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment