Skip to content

Instantly share code, notes, and snippets.

View valentinitnelav's full-sized avatar
🏠
Working from home

Valentin Ștefan valentinitnelav

🏠
Working from home
View GitHub Profile
# Call library data.table
library(data.table)
# ============================================ #
# Prepare data #
# ============================================ #
# Read your data.
DT <- fread("Hawaii_sites.csv")
# Get only lat-long unique records.
# function to get the first n words from a string
get_words <- function(strg, n = 3){
# remove any leading and trailing whitespace
strg <- trimws(strg)
# replace all punctuation with space (except apostrophe)
strg <- gsub(pattern = "[^[:alnum:][:space:]']", replacement = ' ', strg, perl=TRUE)
# merge multiple spaces to single space
strg <- gsub(pattern = "(?<=[\\s])\\s*|^\\s+|\\s+$", replacement = "", strg, perl=TRUE)
# take first n elements when splitting by space
strg <- strsplit(strg, split=" ", fixed=TRUE)[[1]][1:n]
# function to get first name from a string of names (including the particle e.g. von Frisch)
# strg = a string containing names
# NOTE: The function still needs further testing. Use with care!
get_author_name <- function(strg){
# remove any leading and trailing whitespace
strg <- trimws(strg)
# check if string starts with two "nobiliary" particles
logic_2prtcl <- grepl(pattern = "^van der|^von der", x = strg, perl=TRUE, ignore.case = TRUE)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This is an example of plotting a barplot in a pdf device
# Journals usually accept the pdf vector format
# One can also use other vector formats:
# 1) emf() function from “devEMF" package produces Enhanced Metafile (EMF)
# Microsoft Office should recognize EMFs(https://goo.gl/1bxC7G)
# 2) postscript()
# "Microsoft Office is supposed to be able to handle EPS files
# and R postscript files are by default EPS compatible
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This script presents a simple example of plotting using several graphical devices
# For a list of graphical devices offered in R just run ?Devices
# Note that journals usually accept PDF as vector format and TIFF for images
# PDF is the easiest way to use when it comes to creating graphs in vector format.
# Also the EPS (postscript) format is widely accepted by journals.
# For TIFF files, use the "LZW compression" option when saving.
# That will significantly reduce the file size, without adversely affecting the image quality
# (http://esapubs.org/esapubs/preparation.htm#Ill).
# __________ Create data.frame for graticule labels
# use together with graticule of 10 dg step for latitude and 20 dg step for longitude
lbl.Y <- data.frame(lon = rep(c(-175,175), each=17), # 175 dg used to fit latitude labels inside Earth; adjust this value to shift positions of labels
lat = rep(seq(from=80, to=-80, by=-10), times=2))
lbl.Y$dir <- ifelse(lbl.Y$lat == 0, "", ifelse(lbl.Y$lat > 0, "°N", "°S"))
lbl.Y$lbl <- paste0(abs(lbl.Y$lat), lbl.Y$dir)
lbl.X <- data.frame(lon = rep(seq(from=160, to=-160, by=-20), times=2),
lat = rep(c(-85,85), each=17)) # 85 dg used to fit latitude labels inside Earth; adjust this value to shift positions of labels
lbl.X$dir <- ifelse(lbl.X$lon == 0, "", ifelse(lbl.X$lon > 0, "°E", "°W"))
# ======================================================================================
# Creating graticule lines and labels
# ======================================================================================
library(raster)
# first need to create a bounding box
b.box <- as(extent(180, -180, -90, 90), "SpatialPolygons")
# assign CRS
proj4string(b.box) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
# create gridlines baes on the bounding box
# define the function:
Interaction.Plots_fun <- function(MyMatrix.lst, data.lst) {
# storing default par() for reverting par to original/default values
par.default <- par()
# on.exit(par(par.default)) # when exiting function par(), restore to default (? - need to test!)
# par(par.default) # or simply call each time this line
# open a pdf device with desired parameters
pdf(file=paste0("Mueller ", format(Sys.time(), "%Y-%m-%d %H-%M-%p"), ".pdf"), onefile=TRUE, width=24, height=8.27)
# remember that an A4 is 8.27 × 11.69 in. Here you want a landscape format width > height!
@valentinitnelav
valentinitnelav / Shift central (prime) meridian.R
Last active January 1, 2017 10:53
Shift central/prime meridian by splitting the world map in two
# ===================================================================================
# Shift central/prime meridian and plot world map with ggplot
# ===================================================================================
# __________ Load needed libraries __________ #
library(data.table)
library(ggplot2)
library(rgdal)
library(rgeos)
library(maps)
@valentinitnelav
valentinitnelav / Shift central meridian - test, Greenland example.R
Last active January 1, 2017 12:29
Shift central meridian - fails concave polygon & closure of split polygons, Greenland example.R
# ===================================================================================
# Test case - Shift central/prime meridian
# Fails concave polygons and closure of split polygons
# Show case example for Greenland
# ===================================================================================
# __________ Load needed libraries __________ #
library(data.table)
library(ggplot2)
library(maps)