Skip to content

Instantly share code, notes, and snippets.

View Nowosad's full-sized avatar
🌍

Jakub Nowosad Nowosad

🌍
View GitHub Profile
@Nowosad
Nowosad / check-url.R
Created October 31, 2023 18:11 — forked from nanxstats/check-url.R
A general-purpose link checker for R Markdown and Quarto projects https://nanx.me/blog/post/rmarkdown-quarto-link-checker/
#' Flatten copy
#'
#' @param from Source directory path.
#' @param to Destination directory path.
#'
#' @return Destination directory path.
#'
#' @details
#' Copy all `.Rmd`, `.qmd`, and `.md` files from source to destination,
#' rename the `.qmd` and `.md` files with an additional `.Rmd` extension,
library(tmap)
#> Warning: multiple methods tables found for 'area'
data(metro)
metro$group = as.factor(sample(1:5, size = nrow(metro), replace = TRUE))

# two legends
tm_shape(metro) +
  tm_symbols(col = "group", shape = "group")
@Nowosad
Nowosad / st_from_ellipses.R
Created May 29, 2021 14:34
Conversion from a SaTScan text file with ellipses to an sf R spatial object
st_from_ellipses = function(path, crs = "EPSG:25832", res = 30) {
e = read.csv(path)
pnt = sf::st_as_sf(e, coords = c("X", "Y"), crs = crs)
major = pnt$Major
minor = pnt$Minor
angle = pnt$Angle1
rotation = function(a){
r = a * pi / 180 #degrees to radians
matrix(c(cos(r), sin(r), -sin(r), cos(r)), nrow = 2, ncol = 2)
}
# remotes::install_github("mtennekes/tmap")
library(tmap)
library(terra)
#> terra version 1.0.7

# 1 -----------------------------------------------------------------------
elev = rast(system.file("ex/elev.tif", package = "terra"))

tm_shape(elev) +
@Nowosad
Nowosad / sf_change_class
Created November 27, 2018 15:17
How to change the class of variables within an sf object?
library(sf)
library(dplyr)
library(spData)
str(nz)
# change class for one column
nz2 = nz %>%
mutate(Land_area = as.factor(Land_area))
library(gganimate)
#> Loading required package: ggplot2
library(ggplot2)
ggplot(diamonds, aes(cut, carat)) +
        geom_point(col = "red") +
        geom_boxplot() + 
        transition_layers(layer_length = 1, transition_length = 1)
#> Error in `$<-.data.frame`(`*tmp*`, ".phase", value = "raw"): replacement has 1 row, data has 0
@Nowosad
Nowosad / lsm_l_shei2.R
Created May 12, 2018 18:41
lsm_l_shei - test
#' Shannon's Evenness Index
#'
#' @description Evenness of patch types (classes) in the landscape
#'
#' @param landscape Raster* Layer, Stack, Brick or a list of rasterLayers.
#' @return tibble
#'
#' @examples
#' lsm_l_shei(landscape)
#' lsm_l_shei(landscape_stack)
library(sf)                              
#> Linking to GEOS 3.6.1, GDAL 2.1.4, proj.4 4.9.3
library(spData)                          
library(tmap)                            
                                         
tm_shape(nz) +                           
tm_polygons() +                          
tm_compass(position = c("left", "top")) +
tm_scale_bar() 
library(sp)
library(sf)
library(tidyverse)
g = SpatialGrid(GridTopology(c(0, 0), c(1, 1), c(3, 3)))
p = as(g, "SpatialPolygons") %>%
st_as_sf() %>%
mutate(id = row_number()) %>%
as("Spatial")