Skip to content

Instantly share code, notes, and snippets.


Nico Katzke Nicktz

View GitHub Profile
View MWE with image
# 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.
Nicktz / Arranging by column for ggplot
Created May 18, 2021
Arranging by column for ggplot
View 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:
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)
Nicktz / psec_theme.R
Last active Sep 10, 2020
View 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 / cluster_aux.r
Last active Aug 18, 2020
cluster aux loading
View cluster_aux.r
# Source this gist to load functions in local environment that allow dendogram plotting using ggplot2 in R:
#' @title cluster_aux
#' @description Auxilliary functions for doing nice dendograms in R. See here for more:
#' @return Loads several auxilliary functions used for dendogram plotting
#' @examples
#' \dontrun{
#' cluster_aux()
#' mtc <- scale(mtcars)
#'D <- dist(mtc)
Nicktz / SQL connection setup on Linux Server for R
Last active May 14, 2020
SQL connection setup on Linux Server for R
View 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:
First, get Server's IP. In linux / Terminal in Rstudio use
View Safe_Return.portfolio
# 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
View RetEx.R
# Example created with datapasta
df <-
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")),
~A_Shares, ~X_Shares, ~C_Shares, ~A_Price, ~X_Price, ~C_Price,
View geom_dual
# Function built by Nico Katzke (
# 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.:
if(!require(rmsfuns)) install.packages("rmsfuns")
df1 <- tibble(date = rmsfuns::dateconverter(ymd(20170101), ymd(20180101), Transform = "weekdays")[1:100],
View Tidyeval ex
bare to quosure: quo
bare_to_quo <- function(x, var){
x %>% select(!!var) %>% head(1)
bare_to_quo(mtcars, quo(cyl))
## cyl
Nicktz / ggplot templates
Last active Nov 12, 2019
ggplot templates
View ggplot templates
Excellent miscellaneous plotting calls