Skip to content

Instantly share code, notes, and snippets.

View JoachimGoedhart's full-sized avatar
💡
Coding now and then

Joachim Goedhart JoachimGoedhart

💡
Coding now and then
View GitHub Profile
@JoachimGoedhart
JoachimGoedhart / ggplot2_geom_point-shapes
Last active September 26, 2022 20:09
Plotting all available shapes&labels for geom_point()
library(tidyverse)
x <- rep(0:15, 8)
y <- rep(0:7, each=16)
shape <- 0:127
df_shapes <- data.frame(x=x, y=y, shape=shape) %>%
filter(!shape %in% c(26:31)) #Shapes 26-31 are not defined, so we'll remove those
ggplot(data=df_shapes, aes(x,y)) +
// Macro to add a column with the time in the 'Results' window
// The time is calculated from the Frame interval defined by the user
// In this macro the time interval is defined by the variable 'interval'
// Created by Joachim Goedhart with essential input from Jerome Mutterer (@jmutterer)
//Set the interval (alternatively it can be set in ImageJ/FIJI from the menu: [Image > Properties...])
interval=2
Stack.setFrameInterval(interval)
//Calculate the time for each frame and add to the results window
@JoachimGoedhart
JoachimGoedhart / Randomization_test.R
Last active October 14, 2018 18:59
R-script that calculates a p-value using a randomization test to generate the H0 distribution
#Function that returns p-values from randomization/permutation test
require(dplyr)
require(tidyr)
#Set number of randomizations
nsteps <- 1000
#Read the dataframe - the example data used below is available at: https://github.com/JoachimGoedhart/PlotsOfData
df <- read.csv("Data_wide_example.csv")
@JoachimGoedhart
JoachimGoedhart / Read-all-csv-files-into-dataframe.R
Last active November 14, 2022 15:00
Reads all *.csv files from working directory and moves the data into a single dataframe
require(data.table)
require(dplyr)
#Get a list with all csv files from the directory that is set as 'working directory'
filelist = list.files(pattern="*.csv$")
#read all csv files with data.table::fread() and put in df_input_list
df_input_list <- lapply(filelist, fread)
#reading in csv files can also be done using the base R function read.csv(), without needing to load package "data.table":
@JoachimGoedhart
JoachimGoedhart / Tol-qualitative-palettes.R
Last active July 27, 2020 13:43
Generates vectors (for R) that hold a color palette that is colorblind safe
#Several qualitative color palettes that are colorblind friendly
#From Paul Tol: https://personal.sron.nl/~pault/
#Code to generate vectors in R to use these palettes
Tol_bright <- c('#EE6677', '#228833', '#4477AA', '#CCBB44', '#66CCEE', '#AA3377', '#BBBBBB')
Tol_vibrant <- c('#EE7733', '#0077BB', '#33BBEE', '#EE3377', '#CC3311', '#009988', '#BBBBBB')
Tol_muted <- c('#332288', '#88CCEE', '#44AA99', '#117733', '#999933', '#DDCC77', '#CC6677', '#882255', '#AA4499', '#DDDDDD')
@JoachimGoedhart
JoachimGoedhart / Dataframe_with_summary-of-data.R
Created June 8, 2018 20:24
Reads non-tidy time-dependent data and calculates statistics
#Reads a non-tidy (wide) dataframe from csv file and assumes first column to be time, remaining columns measured parameter (Ratio) per condition (Cell).
df_wide <- read.csv("FRET-ratio-wide.csv", na.strings = "")
#Tidy the data, i.e. long format with each row is variable
df_tidy <- gather(df_wide, Cell, Ratio, -Time)
######### Calulcate summary statistics to fill dataframe 'df_summary' ########
# This is base R approach
df_summary <- data.frame(Time=df_wide$Time, n=tapply(df_tidy$Ratio, df_tidy$Time, length), mean=tapply(df_tidy$Ratio, df_tidy$Time, mean))
# somewhat hackish solution to:
# https://twitter.com/EamonCaddigan/status/646759751242620928
# based mostly on copy/pasting from ggplot2 geom_violin source:
# https://github.com/hadley/ggplot2/blob/master/R/geom-violin.r
library(ggplot2)
library(dplyr)
"%||%" <- function(a, b) {