Skip to content

Instantly share code, notes, and snippets.

Avatar

Leif Jonsson lejon

View GitHub Profile
@lejon
lejon / PrintRounded.java
Created Mar 31, 2019
Prints a double array in scientific notation with the same number of digits per index.
View PrintRounded.java
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 Mar 7, 2019
Plot barchart in R
View plot_bar.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 Jan 10, 2019
Plot the distribution of variables in a data frame
View plot_df_distis.R
#' 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 Aug 18, 2018
Plot distributions and overlayed distrobutions using ggplot
View plot_dist.R
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 Aug 6, 2018
Ggplot histogram object from vector of counts
View plot_hist.R
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))