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
# ======================================================================================
# Create a simple world map in Robinson projection with labeled graticules using ggplot
# ======================================================================================
# Set a working directory with setwd() or work with an RStudio project
# __________ Set libraries
library(rgdal) # for spTransform() & project()
library(ggplot2) # for ggplot()
@valentinitnelav
valentinitnelav / Pacific centered world map with ggplot.R
Last active July 12, 2023 08:25
Pacific centered world map with ggplot (change central/prime meridian)
# ======================================================================================
# Pacific centered world map with ggplot
# Enhanced aspect with graticules and labels
# The central/prime meridian can be shifted with any positive value towards west
# Can use any project of known PROJ.4 string
# ======================================================================================
# ~~~~~~~~~~~ Load needed libraries ~~~~~~~~~~~ #
library(data.table)
library(ggplot2)
# Converting coordinates from DMS or DdM formats to decimal
# DMS = "degrees minutes seconds"; DdM = "degrees decimal minutes"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dg2dec <- function(varb, Dg=NA, Min=NA, Sec=NA, SW.Hemisphere="S|W") {
# Dg=decimal, Min=minutes and Sec=seconds;
# NOTE 1 - if the format is "degrees decimal minutes - DdM" (e.g. 40° 26.767′ N) and not
# "degrees minutes seconds - DMS" (e.g. 40° 26′ 46″ N), then call the function only with
# Dg and Min arguments, like dg2dec(varb, Dg="°", Min="′N").
# Same applies when there is no seconds symbol (e.g. 45°12'7.38).
# Note that one should not use blank spaces in Dg, Min or Sec arguments (will return NA).
@valentinitnelav
valentinitnelav / Plot inwards ticks - ggplot.R
Last active October 7, 2021 13:14
Plot with inwards ticks - ggplot
# ==========================================================================
# Example of plot for publication with inwards ticks in ggplot
# ==========================================================================
library(ggplot2)
# ==================================
# create some data
# ==================================
set.seed(1)
@valentinitnelav
valentinitnelav / make_GeodesicBuffer.R
Last active April 12, 2018 02:47
Geodesic buffer for point data
make_GeodesicBuffer <- function(XY.dg, dg.step=5, dst.m, crs){
#######################################################################################################
## Function to make a circle-buffer around given points (long-lat coordinates)
## Is different from rgeos::gBuffer() by the fact that it allows the user to create
## a geodesic buffer with a width expressed in metric units.
## Otherwise the user needs to project and apply an Euclidian buffer with rgeos::gBuffer(),
## which will introduce distortions that vary greatly with latitude and the radius of the circle buffer.
## See also my question addressed here:
## https://gis.stackexchange.com/questions/250389/euclidean-and-geodesic-buffering-in-r
##
# extract long-lat coordinates from a bunch of kmz files
# first unzips the KMZ-s then reads coordinates from each KML with getKMLcoordinates {maptools}
# (worth checking this as well: https://gist.github.com/holstius/6631918 )
library(maptools)
# list the kmz files in a given folder path
KMZs <- list.files(path="Data/kmz-files", pattern="*.kmz", full.names=FALSE)
# unzip each KMZ file and read coordinates with getKMLcoordinates()
# =============================================================================================================
# Scraps the TPL web page for all families of angiosperms and downloads the corresponding *csv files.
# Then grabs the unique species names from this pile of data and builds a custom dictionary for a spell checker.
# =============================================================================================================
# _________ Read the family names from the TPL web page (Angiosperms only) _________ #
# I adapted code from http://www.stat.berkeley.edu/~spector/s133/Readexample.html for parsing through web pages
thepage <- readLines("http://www.theplantlist.org/1.1/browse/A/")
# One can notice that family names are in rows of such patern:
# <i class=\"family\">Piperaceae</i></a>"
@valentinitnelav
valentinitnelav / dotplot for publication - ggplot.R
Last active January 14, 2018 02:44
Create a dotplot for publication - ggplot.R
# ==========================================================================
# Dotplot with CI/error bars for publication - ggplot
# see also: http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/
# ==========================================================================
library(ggplot2)
# ==================================
# create some data
# ==================================
@valentinitnelav
valentinitnelav / Check_taxa_names_Taxonstand&wikitaxa.R
Last active January 3, 2018 16:36
Check some taxa names with Taxonstand & wikitaxa
# -----------------------------------------------------------------------------
# Example to check some taxa names with Taxonstand & wikitaxa
# -----------------------------------------------------------------------------
my_sp <- fread("Output/taxa_to_check_VS.csv")
my_sp
# taxa
# Thereianthus_spicatus
# Eriocnema_fulva
# Hebe_macrocarpa
# Aspalathus_cymbriformis
library(ggplot2)
library(ggsci) # for cool palettes
# read data
my_data <- read.csv("data/chickengrowthdiet.csv")
str(my_data)
# Diet should not be numeric but factor (or categorical)
my_data$Diet <- factor(my_data$Diet)
# -------------------------------------