Skip to content

Instantly share code, notes, and snippets.

View abresler's full-sized avatar

Alex Bresler abresler

View GitHub Profile
@mick001
mick001 / neuralnetR.R
Last active November 26, 2023 19:12
A neural network exaple in R. Full article at: http://datascienceplus.com/fitting-neural-network-in-r/
# Set a seed
set.seed(500)
library(MASS)
data <- Boston
# Check that no data is missing
apply(data,2,function(x) sum(is.na(x)))
# Train-test random splitting for linear model
@slowkow
slowkow / plot_repel.R
Last active May 2, 2018 21:07
Repel text labels away from each other in a ggplot2 figure.
library(ggplot2)
library(FField)
# You'll have to play with repulsion, cex.x, and cex.y to get satisfactory results.
plot_text <- function(x, y, label, repulsion = 1, cex.x = 110, cex.y = 40) {
dat <- data.frame(xpos = x, ypos = y, label = label)
dat$label <- as.character(dat$label)
# Use the FField package to repel the text labels away from each other.
dat <- cbind(
@timelyportfolio
timelyportfolio / Readme.md
Last active March 3, 2018 12:15
interactive d3.js parallel coordinates of finch data

I thought it would be interesting to produce an interactive parcoords version of the parallel coordinates chart in this post Looking After Datasets by Antony Unwin on the Revolution Analytics blog.

example

live example

code

# devtools::install_github("timelyportfolio/parcoords")
# install.packages("dynRB") # for the finch data
data("finch",package="dynRB")
library(ggplot2)
library(dplyr)
library(tidyr)
library(stringr)
library(scales)
library(gridExtra)
library(grid)
# use the NPR story data file ---------------------------------------------
# and be kind to NPR's bandwidth budget
@timelyportfolio
timelyportfolio / Readme.md
Last active August 29, 2015 14:28 — forked from jalapic/gana.csv
Heatmap of touches (in rbokeh)

@jalapic did these really nice set of touch heatmap charts. On Twitter, there was a discussion how to do this interactively with d3.js or some other JavaScript. I definitely have some ideas how to accomplish this, but for now let's see how we can kind of do it in rbokeh. There is just a small issue with palette selection in Bokeh that does not let us allow a no color that prevents us from getting really close.

## https://gist.github.com/jalapic/3616c0197ece24060e99
library(rbokeh)
library(dplyr)

gana <- read.csv("https://gist.githubusercontent.com/jalapic/3616c0197ece24060e99/raw/2bc2361c5fceb9be80dece9b7ffa14d129a3edfb/gana.csv")

figure() %>%
@jalapic
jalapic / gana.csv
Created August 23, 2015 19:54
Heatmap of touches
x y
2 90
7 15
15 3
18 58
20 20
19 33
36 4
36 5
37 19

Main differences are:

  • All the initial data acquisition & munging is kept in one pipeline and the data structure is kept as a tbl_df
  • I ended up having to use html_session since the site was rejecting the access (login req'd) w/o it
  • The for loop is now an apply iteration and pbapply gives you a progress bar for free which is A Good Thing given how long that operation took :-)
  • The move to pbapply makes it possible to do the row-binding and left-joining in a single pipeline, which keeps everything in a tbl_df.
  • Your for solution can be made almost as efficient if you do a img_list <- vector("list", 150) so the list size is pre-allocated.

BTW: the popup code is brilliant and tigris is equally as brilliant!

@jeroen
jeroen / jsonlite_nested.S
Last active August 29, 2015 14:23
Nested data with jsonlite
# Nested data
library(jsonlite)
mydata <- jsonlite::fromJSON('https://gist.githubusercontent.com/mbostock/4063269/raw/a05a94858375bd0ae023f6950a2b13fac5127637/flare.json')
# Root node
mydata$name
mydata$children$name
# First child
mydata$children[[1,"name"]]
@timelyportfolio
timelyportfolio / Readme.md
Last active June 26, 2017 23:25
R tables to networks for visualization

added as d3_table() in d3r


Although very handy and useful, I think most new R developers do not even know that tables exist. They are a little tricky to use, but they offer some nice functionality. These tables become even more useful when we can visualize them as networks. This little bit of code is a rough first attempt at converting tables to network nodes and edges.

live example

@hrbrmstr
hrbrmstr / ggplotrbokeh.R
Created May 26, 2015 21:57
ggplot <-> rbokeh
library(ggplot2)
library(rbokeh)
library(htmlwidgets)
structure(list(wk = structure(c(16069, 16237, 16244, 16251, 16279,
16286, 16300, 16307, 16314, 16321, 16328, 16335, 16342, 16349,
16356, 16363, 16377, 16384, 16391, 16398, 16412, 16419, 16426,
16440, 16447, 16454, 16468, 16475, 16496, 16503, 16510, 16517,
16524, 16538, 16552, 16559, 16566, 16573), class = "Date"), n = c(1L,
1L, 1L, 1L, 3L, 1L, 3L, 2L, 4L, 2L, 3L, 2L, 5L, 5L, 1L, 1L, 3L,