Skip to content

Instantly share code, notes, and snippets.

View eliocamp's full-sized avatar

Elio Campitelli eliocamp

View GitHub Profile
@eliocamp
eliocamp / windrose.R
Last active September 10, 2017 15:08
Example of a windrose on ggplot2
library(ggplot2)
library(data.table)
# Datos de este link: http://midcdmz.nrel.gov/apps/plot.pl?site=NWTC&start=20010824&edy=26&emo=3&eyr=2062&year=2013&month=1&day=1&endyear=2013&endmonth=12&endday=31&time=0&inst=21&inst=39&type=data&wrlevel=2&preset=0&first=3&math=0&second=-1&value=0.0&user=0&axis=1
wind <- fread("~/Downloads/20130101.csv")
# La función cut me da los intervalos en orden decresciente, pero
# los necesito en orden cresciente.
ReverseCut <- function(x, ...) {
f <- cut(x, ...)
@eliocamp
eliocamp / fig.ncol_test.Rmd
Created October 11, 2017 01:34
Test for fig.ncol
---
title: "Untitled"
author: "Elio"
output:
pdf_document:
keep_tex: yes
fig_caption: yes
header-includes:
- \usepackage{subfig}
---
# a <- 6371000
lonlat2xy <- function(lon, lat, a = 6371000) {
A <- pi/2*a*(1 - lat/90)
rot <- 0
x <- cos((lon + rot)*pi/180)*A
y <- sin((lon + rot)*pi/180)*A
return(list(x = x, y = y))
}
@eliocamp
eliocamp / circular-color.R
Created January 24, 2018 19:30
Use circular colorscale in R
library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100))
df$dir <- with(df, atan2(y, x))
df$t <- 1:nrow(df)
## The default is, of course, ridiculous.
(g <- ggplot(df, aes(dir, t)) +
geom_point(aes(color = dir)) +
@eliocamp
eliocamp / relief-shade.R
Last active January 25, 2018 16:20
Relief Shade in R
library(data.table)
library(ggplot2)
data(volcano)
volcano <- as.data.table(melt(volcano, varnames = c("x", "y"),
value.name = "h"))
volcano[, c("dx", "dy") := metR::Derivate(h ~ x + y)] # from my package. It calculates directional derivatives.
volcano[, angle := atan2(-dy, -dx)]
@eliocamp
eliocamp / geom_relief.R
Last active February 3, 2018 20:42
Geom for relief shading.
geom_relief <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
raster = TRUE,
interpolate = TRUE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
ggplot2::layer(
data = data,
@eliocamp
eliocamp / notify.R
Created May 18, 2018 18:28
Notifications from R (only for linux)
notify <- function(title = "title", text = NULL, time = 2) {
time <- time*1000
system(paste0("notify-send ", title, " ", text, "-t ", time, " -a rstudio"))
}
notify_after <- function(expression, ...) {
expression <- eval(expression)
notify(title = "Run\\ ended", ...)
return(expression)
}
library(ggplot2)
url <- "https://ih0.redbubble.net/image.512523322.6908/flat,800x800,075,f.jpg"
file <- tempfile()
download.file(url, file)
im <- jpeg::readJPEG(file)
im <- apply(im, c(1, 2), sum)
im <- im/3
ys <- nrow(im)
xs <- ncol(im)
@eliocamp
eliocamp / erebus.md
Last active June 8, 2018 09:09
Plot a terrain relief of Erebus volcano with contour lines.
library(data.table)
library(ggplot2)
library(metR)

out.file <- "~/Downloads/erebus_atm_2001_dem_v5.tif"
if (!file.exists(out.file)) {
   # data => https://www.pgc.umn.edu/data/elevation/
   # ~ 90Mb   
   download.file("http://data.pgc.umn.edu/elev/dem/atm/2001/erebus_atm_2001_dem_v5.tif",
geom_relief <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
anglebreaks = 60,
sunangle = 60,
shadow = TRUE,
maxsearch = 100,
lambert = TRUE,
zscale = 1,
multicore = TRUE,