Skip to content

Instantly share code, notes, and snippets.

View Nicktz's full-sized avatar

Nico Katzke Nicktz

View GitHub Profile
# This is the code to generate .RData file.
# Note - this will create the environment which, if loaded, works as is on my PC.
# See environment on right of your screen - it saves those elements.
# Keep this environment ONLY relevant to what you need help with (don't clutter - filter your df)
# E.g.
rm(list=ls())
library(tidyverse)
@Nicktz
Nicktz / Arranging by column for ggplot
Created May 18, 2021 06:07
Arranging by column for ggplot
# This function takes in a dataframe, a column name (to be arranged), and the order with which to arrange it.
# Use example:
library(tidyverse)
set.seed(123)
df <- tibble::tibble( Charcol = LETTERS[1:10], Numcol = rnorm(10))
order <- df %>% arrange(desc(Numcol)) %>% pull(Charcol) %>% unique
plot_orderset <- function(df, Column, Order) {
df[,Column][[1]] <- factor(df[,Column][[1]], levels = Order)
df
@Nicktz
Nicktz / psec_theme.R
Last active September 10, 2020 10:45
psec_theme.R
#' @title theme_psec1
#' @description Plotting theme for ggplot: with boundary
#' @param FontType Check available fonts using: windowsFonts()
#' @return plot theme
#' @examples
#' g + theme_psec1(title.size = ggpts(35), axis.size.title = ggpts(35), grid_Col = "#FFFFFF") # Remove grids
#' g + theme_psec1(title.size = ggpts(35), axis.size.title = ggpts(35))
#'
#' # Custom Caption used as:
#' # labs(caption= "First~line \n italic('and a second,')~bold('fancy one') \n 'also,'~integral(f(x)*dx, a, b)~'for good measure'")
@Nicktz
Nicktz / cluster_aux.r
Last active September 18, 2023 10:54
cluster aux loading
# Source this gist to load functions in local environment that allow dendogram plotting using ggplot2 in R.
# Written by Nico Katzke (nfkatzke@gmail.com)
#' @title cluster_aux
#' @description Auxilliary functions for doing nice dendograms in R. See here for more info: https://atrebas.github.io/post/2019-06-08-lightweight-dendrograms/
#' @return Loads several auxilliary functions used for dendogram plotting
#' @examples
#' \dontrun{
#' cluster_aux()
#' mtc <- scale(mtcars)
@Nicktz
Nicktz / SQL connection setup on Linux Server for R
Last active May 14, 2020 09:07
SQL connection setup on Linux Server for R
# Shows setup for both SQL Server and MySQL
## SQL Server
Connecting to SQL server from linux (Useful answer: https://stackoverflow.com/a/51266453/4198868):
First, get Server's IP. In linux / Terminal in Rstudio use
ping PSC-CPT-SQL2
# Copy the below into your R console and run the code to see illustration of safe return calculation.
# Motivation:
# Depending on your data structure and assumptions - you should choose how to square your return and weight dates.
# I mostly want to merge on the day returns to on the day weights.
# E.g.
# date Stock Returns weights
# 2018-04-01 XXX 0.025 0.05
@Nicktz
Nicktz / RetEx.R
Last active January 4, 2019 09:19
# Example created with datapasta
library(tidyverse)
library(lubridate)
library(tbl2xts)
df <-
bind_cols(
date = as.Date(c("2018-01-01", "2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05", "2018-01-06", "2018-01-07", "2018-01-08", "2018-01-09", "2018-01-10", "2018-01-11", "2018-01-12", "2018-01-13", "2018-01-14", "2018-01-15", "2018-01-16", "2018-01-17", "2018-01-18", "2018-01-19", "2018-01-20")),
tibble::tribble(
~A_Shares, ~X_Shares, ~C_Shares, ~A_Price, ~X_Price, ~C_Price,
@Nicktz
Nicktz / geom_dual
Last active September 12, 2018 12:37
# Function built by Nico Katzke (nfkatzke@gmail.com)
# geom_dual : Dual Axis ggplot function - specifically for time-series (or dataframe with a column "date" or "Date"
# Provide the function with two dataframes that have overlapping date columns, where the values to be plotted are given by Value.
# E.g.:
library(tidyverse)
library(lubridate)
if(!require(rmsfuns)) install.packages("rmsfuns")
df1 <- tibble(date = rmsfuns::dateconverter(ymd(20170101), ymd(20180101), Transform = "weekdays")[1:100],
From: http://feedproxy.google.com/~r/RBloggers/~3/xIaQcw7UIFs/
library(tidyverse)
bare to quosure: quo
bare_to_quo <- function(x, var){
x %>% select(!!var) %>% head(1)
}
bare_to_quo(mtcars, quo(cyl))
## cyl
@Nicktz
Nicktz / ggplot templates
Last active November 12, 2019 21:26
ggplot templates
http://www.ggplot2-exts.org/gallery/
Excellent miscellaneous plotting calls