# Install and load necessary packages
# install.packages(c("ggplot2", "raster"))
library(tidyverse)
library(raster)
library(rasterVis)
Overview
How to color a Raster Map
with R.
<- "data/SR_50M_alaska_nad.tif"
alaska <- raster(alaska)
alaska_raster <- rep(RColorBrewer::brewer.pal(n = 8, name = "RdBu"),200)
ctab colortable(alaska_raster) <- ctab
gplot(alaska_raster, maxpixels = 10e5) +
geom_tile(aes(fill = factor(value))) +
scale_fill_manual(values = ctab,guide = "none") +
coord_equal()+
::theme_map() ggthemes
<- "data/SR_50M_alaska_nad.tif"
alaska <- raster(alaska) alaska_raster
<- as.data.frame(rasterToPoints(alaska_raster))
alaska_df <- unique(alaska_df$SR_50M_alaska_nad) val
Fonts:
library(sysfonts)
font_add_google("Raleway","Raleway")
::showtext_auto() showtext
Colors:
<- c("#001F3F","#008080","#00BCD4","#4CAF50",
colors "#8BC34A","#FFD700")
Map:
<- gplot(alaska_raster, maxpixels = 10e5) +
map geom_tile(aes(fill = value)) +
scale_fill_gradientn(colors=rev(colors))+
coord_equal()+
labs(title="Alaska",
subtile="#30DayMapChallenge 2023 Day 21 Raster",
caption="Colors indicate the high and low level of elevation in Alaska\nDataSource: QGis Raster Sample Data | Map: @fgazzelloni",fill="Elevation")+
::theme_map()+
ggthemestheme(text=element_text(family="Raleway",color="#f6f7f9",
size=40),
panel.grid = element_line(color="#f6f7f9"),
plot.title = element_text(hjust=0.5,vjust=-0.1,
size=120,
face="bold"),
plot.caption = element_text(lineheight = 0.4),
legend.position = "bottom",
legend.background = element_blank(),
legend.text = element_text(color="#f6f7f9",
lineheight = 0.4),
plot.background = element_rect(color="grey50",fill="grey50"),
panel.background = element_rect(color="grey50",fill="grey50"))
::showtext_end() showtext
ggsave(map,"day21_raster2.png",
dpi=320,
bg="grey50")