Skip to content

Instantly share code, notes, and snippets.

View jokergoo's full-sized avatar
🚀

Zuguang Gu jokergoo

🚀
View GitHub Profile
build.position.index = function(x, by = 5) {
segment = round((x/(10^by)))*10^by
se = unique(segment)
mat = matrix(nrow = length(se), ncol = 2)
for(i in seq_along(se)) {
mat[i, ] = range(which(segment == se[i]))
}
list(mat = mat, segment = se, by = 10^by)
}
assignment = function(x, k = 4) {
names(x) = seq_along(x)
y = vector("list", k)
ysum = sapply(y, function(x) 0)
flag = 0
while(length(x)) {
if(flag) {
# == title
# Simple template for text interpolation
#
# == description
# I like text interpolation in Perl. But in R, if you want to connect plain text and variables,
# you need to use `paste` or functions in `stringr`. However, if there are so many variables or
# or many quotes in the string you want to construct, it would be a little terrible.
# So, this function allows you to construct strings as Perl style.
#
# == param
@jokergoo
jokergoo / color.pal.R
Last active December 19, 2015 06:29
linear interpolation between colors
color.pal = function(x, col = c("green", "black", "red"), breaks = c(-5, 0, 5)) {
if(length(col) != length(breaks)) {
stop("Length of col should be equal to the length of breaks.\n")
}
# change col represented as strings to RGB space
col_section = sapply(col, function(x) as.vector(col2rgb(x)))
col_section = t(col_section)
@jokergoo
jokergoo / density.heatplot.R
Last active December 19, 2015 06:09
use heatmap to represent density distribution
require(RColorBrewer)
# since gene expression data always contains a lot of samples.
# If one want to see distributions of samples, using boxplot would be not so clear.
# The function use continuous colors to represent density distributions of expression
# values in samples and can make a better illustration of the data.
heatplot = function(x, col=rev(brewer.pal(10, "Spectral")), draw.quantiles = TRUE, align = TRUE, each = FALSE, ...) {
if(is.vector(x) && class(x) != "list") {
x = as.matrix(x)