Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Dorman michaeldorman

View GitHub Profile
View ggplot2 + sf example
library(ggplot2)
library(sf)
# Prepare layer
dat = data.frame(
name = c("Beer-Sheva Center", "Beer-Sheva University", "Dimona"),
lon = c(34.79844, 34.81283, 35.01163),
lat = c(31.24329, 31.26028, 31.06862)
)
dat = st_as_sf(dat, coords = c("lon", "lat"), crs = 4326)
View transparent colors in ggplot2
library(tidyverse)
library(wesanderson)
mean_steps <- read.csv("data.csv", stringsAsFactors = FALSE)
cols = c("#FF0000", "#00A08A", "#F2AD00")
cols2 = paste0(cols, "80")
cols = c(rbind(cols2, cols))
ggplot(data=mean_steps,aes(x = trial, y = mean_same, color = paste(block, actor))) +
stat_summary(fun="mean",position=position_dodge(width=0.1),
@michaeldorman
michaeldorman / st_rasterize_example.R
Created Jan 2, 2021
Rasterizing points, lines and polygons to raster with `st_rasterize`, using the default algorithms (top) and `options="ALL_TOUCHED=TRUE"` (bottom).
View st_rasterize_example.R
library(stars)
multipoint = st_as_sfc("MULTIPOINT ((10 40), (40 30), (20 20), (30 10))")[[1]]
multilinestring = st_as_sfc("MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))")[[1]]
multipolygon = st_as_sfc("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))")[[1]]
dat = c(st_sfc(multipoint), st_sfc(multilinestring), st_sfc(multipolygon))
dat = st_sf(dat, data.frame(value = 1, type = c("Points", "Lines", "Polygons")))
grid = st_as_stars(st_bbox(st_buffer(dat, 3)), dx = 3, dy = 3)
View arrange stars plots on page
library(stars)
library(classInt)
# Data
r = read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))
# Plot
b = classIntervals(r[[1]], 10, "equal")
b = b$brks
for(i in 1:dim(r)[3]) {
View script1_mean_ndvi_30m.js
// US area polygon
var pol = ee.Geometry.Polygon([ [ [ -80.919630492025277, 30.699135009797704 ], [ -80.871245451372644, 30.336014265533166 ], [ -80.762754341973988, 29.906426353226227 ], [ -80.504997201528766, 29.330160083879278 ], [ -80.083106700408266, 28.673401576509857 ], [ -80.070233511479728, 28.651960676800226 ], [ -80.058638153313169, 28.629988680787292 ], [ -80.04835227119456, 28.607547474440747 ], [ -80.039403769248054, 28.584700211077717 ], [ -80.031816742625793, 28.561511131077758 ], [ -80.025611421049575, 28.538045379128882 ], [ -80.020804123732901, 28.514368819546309 ], [ -80.0174072256752, 28.490547850204102 ], [ -80.01542913528462, 28.466649215616201 ], [ -80.0148742832517, 28.442739819698442 ], [ -80.015743122564871, 28.418886538736928 ], [ -80.018032139528088, 28.395156035079435 ], [ -80.021733875613705, 28.371614572058188 ], [ -80.026836959956768, 28.348327830641338 ], [ -80.033326152273503, 28.32536072829911 ], [ -80.041182395964384, 28.302777240558235 ], [ -80.050382881141516, 28.2806402
View plot_streets.R
library(sf)
library(osmdata)
location = "Tel-Aviv, Israel"
# Get data
q = opq(bbox = location)
dat = add_osm_feature(q, key = "highway")
dat = osmdata_sf(dat)
lines = dat$osm_lines
View coord_sf_lim.R
library(sf)
library(ggplot2)
# Data
nc = st_read(system.file("shape/nc.shp", package="sf"))
map = nc
dat = nc[5, ]
# Plot 1
ggplot() +
@michaeldorman
michaeldorman / packages_proportion.R
Last active Apr 25, 2020
Calculate top 15 commonly used R packages given a directory with R scripts
View packages_proportion.R
# Directory with R scripts
setwd("~/Sync/BGU")
# Prepare data
files = list.files(pattern = "\\.R$", recursive = TRUE)
dat = lapply(files, readLines)
dat = lapply(dat, function(x) x[grepl("library\\(", x)])
dat = lapply(dat, function(x) gsub(".*library\\(", "", x))
dat = lapply(dat, function(x) gsub("\\).*", "", x))
dat = lapply(dat, function(x) gsub('"', '', x))
View map.geojson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View index.html
<!DOCTYPE html>
<html>
<head>
<title>Simple Leaflet Map</title>
<meta charset="utf-8" />
<link
rel="stylesheet"
href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css"
/>
</head>