Skip to content

Instantly share code, notes, and snippets.

# Using the fivethirtyeight package
library(fivethirtyeight)
library(tidyr)
library(tibble)
library(dplyr)
# See names of columns
names(murder_2015_final)
# Getting Started with tidyverse in R
library(tidyr)
library(tibble)
key_value <- tribble(
~row, ~key1, ~key2, ~key3, # These are the names of the columns (indicated with ~)
"1", "1_value_1","1_value_2","1_value_3", # Row 1
"2", "2_value_1", "2_value_2", "2_value_3", # Row 2
"3", "3_value_1", "3_value_2", "3_value_3" # Row 3
# How to create a line chart in R
library(ggplot2)
venezuela.data <- read.csv(file="venezuela.csv")
p1 <- ggplot() +
geom_line(aes(y = units, x = year), data = venezuela.data) +
scale_x_continuous(breaks=seq(2002,2016,2)) +
theme(text=element_text(family="Tahoma"))
# Geocoding a csv column of "addresses" in R
#load ggmap
library(ggmap)
# Select the file from the file chooser
fileToLoad <- file.choose(new = TRUE)
# Read in the CSV data and store it in a variable
origAddress <- read.csv(fileToLoad, stringsAsFactors = FALSE)
// example coordinates
var coords = [34.052108, -118.245678];
var sql = new cartodb.SQL({ user: 'latimes' });
sql.execute("SELECT ST_Distance(the_geom::geography, ST_SetSRID(ST_Point("+ coords[1] +", "+ coords[0] +"),4326)::geography) as fDistance FROM socal_freeways")
.done(function(data) {
// distance in feet rounded to nearest 10
var distFeet = Math.round((data.rows[0].fdistance * 3.28084)/10)*10;
<figure class="graphic-wrapper">
<h2 class="graphic-hed">Thousands of homes approved near L.A. freeways</h2>
<p class="graphic-chatter" style="text-align:center;">Use the slider to see where the city has issued building permits within a 1,000 feet of a freeway since 2005.</p>
<h2 id="year-selected">2005</h2>
<input type="range" step="0" id="retirement-slider" class="graphic-slider" min="0" max="11" value="0">
<div class="slider-map-container">
<ul class="map-images">
<li><img src="/projects/la-me-freeway-pollution/static/img/timeline-slider-2005-01.png"></li>
/** choropleth visualization */
#clean_merge{
polygon-fill: #FFFFB2;
polygon-opacity: 1;
line-color: #FFF;
line-width: 0;
line-opacity: 0;
}
#clean_merge [ estimated_age_adjusted_death_rate_11_categories_in_ranges <= 20.1] {
function tick(e) {
// Push different nodes in different directions for clustering.
var k = 6 * e.alpha;
nodes.forEach(function(o, i) {
o.y += i & 1 ? k : -k;
o.x += i & 2 ? k : -k;
});
node.attr("cx", function(d) { return d.x; })
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.js"></script>
<style>
path.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
var attributes = [
{"name": "concussions", "hex": "#0000ff"}
]
// instantiate d3plus
var visualization = d3plus.viz()
.container("#viz") // container DIV to hold the visualization
.data(sample_data) // data to use with the visualization
.type("line") // visualization type
.id("name") // key for which our data is unique on