Skip to content

Instantly share code, notes, and snippets.

View JEFworks's full-sized avatar

Jean Fan JEFworks

View GitHub Profile
@JEFworks
JEFworks / stairway_to_heaven.R
Last active July 17, 2024 15:25
Using R to play Stairway to Heaven with different instruments
# playing stairway to heaven using R
# hand transcribed notes
notes <- list(
'A3', 'C4', 'E4', 'A4',
c('B4', 'G#3'), 'E4', 'C4', 'B4',
c('C4', 'G3'), 'E4', 'C4', 'C5',
c('F#4', 'F#3'), 'D4', 'A3', 'F#4',
c('E4', 'F3'), 'C4', 'A3', 'C4',
NA, 'E4', 'C4', 'A3',
@JEFworks
JEFworks / fireworks.R
Last active July 4, 2024 22:19
Using gganimate to create an animation of fireworks over a background image
## following https://alistaire.rbind.io/blog/fireworks/
## but manually convert to polar coordinates to be able to add
## background image, which cannot be used with coord_polar()
library(ggplot2)
library(gganimate)
theme_set(theme_void())
## create set of points in polar coordinates
set.seed(0)
@JEFworks
JEFworks / gganimate_test.R
Last active September 20, 2023 17:29
Visualizing parameters for clustering PBMCs using gganimate
##################### Visualizing parameters for clustering PBMCs using gganimate
library(ggplot2)
library(gganimate)
## PBMC dataset
library(MUDAN)
data("pbmcA")
## Downsample to run faster
cd <- MUDAN::cleanCounts(pbmcA)[, 1:2000]
@JEFworks
JEFworks / hatching.R
Last active March 5, 2019 00:00
Hatching photo filter
#' Hatching photo filter
#'
#' @param img matrix representation of black and white image
#' @param N number of points to be used for hatching
#' @param size average size of points
#' @param var variability in size of points
#' @param step step size between shades of grey
#' @param pch point shape (note pch=4 is a hatch but other shapes can be used as well)
#'
#' @examples
@JEFworks
JEFworks / flickr.R
Created August 12, 2018 19:43
Flickr API usage in R
## Use flickr API to get 20 public images for flowers
data1 = paste0(readLines('https://api.flickr.com/services/feeds/photos_public.gne?tags=lilacs'), collapse="")
data2 = paste0(readLines('https://api.flickr.com/services/feeds/photos_public.gne?tags=cherryblossoms'), collapse="")
data3 = paste0(readLines('https://api.flickr.com/services/feeds/photos_public.gne?tags=marigolds'), collapse="")
data4 = paste0(readLines('https://api.flickr.com/services/feeds/photos_public.gne?tags=daffodils'), collapse="")
dataLines = c(
strsplit(data1, "<entry>")[[1]][-1],
strsplit(data2, "<entry>")[[1]][-1],
strsplit(data3, "<entry>")[[1]][-1],
strsplit(data4, "<entry>")[[1]][-1]
@JEFworks
JEFworks / bullseye.R
Last active July 31, 2018 00:09
Bull's eye color frequency plots
library(jpeg)
library(rvest)
## Image urls
urls <- c(
"http://www.instagram.com/p/BWIKgiuADnP/media/?size=m",
"http://www.instagram.com/p/BXY2LnqgB-g/media/?size=m",
"http://www.instagram.com/p/BZhL7zlg0cE/media/?size=m",
"http://www.instagram.com/p/BaHDmk5AOYe/media/?size=m",
"http://www.instagram.com/p/BcDKJywAMh4/media/?size=m",
@JEFworks
JEFworks / dither.R
Last active October 18, 2024 17:23
A fast Floyd-Steinberg dithering filter in C++ using Rcpp
library(Rcpp)
#' Floyd-Steinberg dithering
#'
#' @description Rcpp C++ function for Floyd-Steinberg dithering
#' More info: https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering
#' Note, this is a C++ translation of the FloydConvolution function by Todos Logos
#' from https://www.r-bloggers.com/r-is-a-cool-image-editor-2-dithering-algorithms/
#'
#' @param x Greyscaled 2D image matrix
@JEFworks
JEFworks / pointillism.R
Last active July 30, 2018 12:48
Pointillism filter: Convert an image (RGB array) into array of points in a style reminiscent of pointillism
#' Pointillism filter
#'
#' @description Convert an image (RGB array) into array of points in a style reminiscent of pointillism
#'
#' @param img Image array.
#' @param k Point size. Bigger k means bigger point / less detail.
#' @param seed Random seed for voxel color sampling.
#'
#' @examples {
#' ## Read image
@JEFworks
JEFworks / connectedBarplot.R
Last active September 21, 2017 18:05
Connected Barplot in R for Series Data Visualization
#' Plots a series of barplots and connects them
#' Modified from https://stackoverflow.com/questions/22560850/barplot-with-connected-series
#'
#' @param dat NxM matrix with N rows as features and M columns as samples
#' @param color Vector of N colors
#' @param space Space between barplots
#' @param alpha Alpha for area connecting barplots
#'
#' @examples
#' dat <- matrix(rnorm(100),10,10)