Skip to content

Instantly share code, notes, and snippets.

Avatar

Nico Katzke Nicktz

View GitHub Profile
@Nicktz
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:
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 Sep 10, 2020
psec_theme.R
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
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: 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)
#'D <- dist(mtc)
@Nicktz
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: https://stackoverflow.com/a/51266453/4198868):
First, get Server's IP. In linux / Terminal in Rstudio use
ping PSC-CPT-SQL2
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
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,
View geom_dual
# 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],
View Tidyeval ex
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 Nov 12, 2019
ggplot templates
View ggplot templates
http://www.ggplot2-exts.org/gallery/
Excellent miscellaneous plotting calls
@Nicktz
Nicktz / 0_reuse_code.js
Created Aug 22, 2017
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console