Skip to content

Instantly share code, notes, and snippets.

@oousmane
Last active April 8, 2023 23:45
Show Gist options
  • Save oousmane/3be51dafa1c76a94a902c8edd64144ef to your computer and use it in GitHub Desktop.
Save oousmane/3be51dafa1c76a94a902c8edd64144ef to your computer and use it in GitHub Desktop.
This code provide a way to read in IDRISI Raster Format with terra and plot with ggplot2
library(terra) # Spatial Data Analysis
library(tidyterra) # 'tidyverse' Methods and 'ggplot2' Helpers for 'terra'
library(ggplot2) # For Data Visualisations Using the Grammar of Graphics
library(rgeoboundaries) # For Political Administrative Boundaries
library(ggspatial) # Spatial Data Framework for ggplot2
library(ggtext) # Improved Text Rendering Support for 'ggplot2'
library(ggsflabel) # for spatial labeling
x <- terra::rast(x = "/Volumes/OUSMANE/exo_BSD/semis_fin_07/sem0173.rst")
y <- classify(x,c(0,1,2,3,4,5,6,7,8))
ne_adm1 <- geoboundaries(country = "Niger",adm_lvl = 1)# need internet
ne_adm0 <- geoboundaries(country = "Niger",adm_lvl = 0)
ggplot()+geom_spatraster(data = y)+geom_sf(data = ne_adm1,fill=NA)+
geom_sf(data = ne_adm0,fill=NA,linewidth=1)+
geom_sf_label_repel(data = ne_adm1,aes(label = shapeName))+
scale_fill_viridis_d("Légende",
na.value = NA,
labels = c(paste("date",1:7),"No data")
)+
labs(title = "Date de semis optimales à la décade de Juillet 2001",
subtitle = "Sortie de simulation DHC-CP",
caption ="© 2023 - **Ousmane Ouedraogo**, _oousmane.github.io_",
x = "Longitude (°)",
y = "Latitude (°)")+
scale_x_continuous(limits = c(0,16),breaks = seq(0,16,2))+
theme_classic(base_size = 14)+
theme(legend.key = element_rect(color = "black"),
legend.background = element_rect(color = "black"),
plot.title = element_text(face = "bold"),
plot.caption = element_markdown(),
panel.grid.major = element_line(linetype = "dashed",color ="grey"),
panel.background = element_rect(fill = "gray90")
)+
annotation_north_arrow(style = north_arrow_nautical(),
location = "tl")+
annotation_scale(text_face = "bold
",location = "br")
@oousmane
Copy link
Author

oousmane commented Apr 8, 2023

The ouput of this code is awesome
plot

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