Skip to content

Instantly share code, notes, and snippets.

View lejon's full-sized avatar

Leif Jonsson lejon

View GitHub Profile
@lejon
lejon / PrintRounded.java
Created March 31, 2019 10:51
Prints a double array in scientific notation with the same number of digits per index.
public static String toRoundedString(double[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
@lejon
lejon / plot_bar.R
Created March 7, 2019 21:47
Plot barchart in R
plot_bar <- function(vals, title = "Histogram") {
fdf <- as.data.frame(vals)
pl <- (ggplot(fdf,aes(x=1:nrow(fdf),y=vals,ymin=0,ymax=vals))
+ geom_bar(colour="white",fill="blue",stat="identity")
+ ggtitle(title)
+ theme(plot.title=element_text(family="Arial", size=10))
+ theme(axis.text.x = element_text(angle = 90, hjust = 1))
)
@lejon
lejon / plot_df_distis.R
Last active January 10, 2019 05:57
Plot the distribution of variables in a data frame
#' plot_df_dists
#'
#' Takes a dataframe and produces plots of the distributions of
#' all variables in the dataframe. Variables containing only
#' unique values should be filtered out before calling this
#' function since it is quite pointless plotting distributions
#' for these types of variables and it takes a long time too
#'
#' @param df input data frame
#'
@lejon
lejon / plot_dist.R
Created August 18, 2018 10:11
Plot distributions and overlayed distrobutions using ggplot
plot_dist <- function(dist_fun, dist_args, range) {
pl <- ggplot(data = data.frame(x = range), aes(x)) +
stat_function(fun = dist_fun, n = 101, args = dist_args) + ylab("") +
scale_y_continuous(breaks = NULL)
}
plot_overlay_dist <- function(dist_funs, dist_args, range) {
pl <- ggplot(data = data.frame(x = range), aes(x)) + scale_y_continuous(breaks = NULL)
fcnt <- 1;
for(dfun in dist_funs) {
@lejon
lejon / plot_hist.R
Created August 6, 2018 11:36
Ggplot histogram object from vector of counts
library(ggplot2)
plot_hist <- function(counts, title = "Histogram") {
fdf <- as.data.frame(table(counts))
pl <- (ggplot(fdf,aes(x=counts,y=Freq,ymin=0,ymax=Freq))
+ geom_bar(colour="white",fill="blue",stat="identity")
+ ggtitle(title)
+ theme(plot.title=element_text(family="Arial", size=10))
+ theme(axis.text.x = element_text(angle = 90, hjust = 1))