Skip to content

Instantly share code, notes, and snippets.

@mehak-sachdeva
Last active November 29, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mehak-sachdeva/0d28e673501ef245d87b0d9c200ef98e to your computer and use it in GitHub Desktop.
Save mehak-sachdeva/0d28e673501ef245d87b0d9c200ef98e to your computer and use it in GitHub Desktop.

CARTO at GIS Day - Yale

November 17, 2016

Michelle Ho, mho@carto.com

Mehak Sachdeva, mehak@carto.com, @MehakSachdeva_

This link is here: http://bit.ly/1117-yale

What is CARTO?

  • It depends on how you use it ;)
    • Builder -- Cloud-based Analysis and mapping platform
    • Engine -- APIs for building custom applications
  • Geo data matters

Inspiring Maps I like

Data Science projects


Demo analysis workflow

Election Choropleth Walkthrough

Welcome to CARTO Builder!

A peek at the final map for the first session:

screen shot 2016-11-17 at 10 28 37 am

Data

For this map, we are using the same data as the Guardian. We went through several steps to grab the raw data and join it with county and/or congressional districs (based on the reporting unit). We are also using some reference layers for state boundaries and labels.

To start, add the following layers:

SQL gist

Attributes

Let's take a look at what attributes we have in the data.

  • To follow the NY Times inspiration map, we calculated the percentage votes for each party as well as the county winner (democratic or republican) for each reporting unit
  • For this map we will symbolize two variables
  • The county winner for each unit (county_winner)
  • And using a color ramp, the percentage of votes for each party (pct_dem and pct_gop)

Choropleth Styling

  • Let's start by styling our choropleth map
  • Click on the Style tab
  • Style by value
  • Search for pct_dem
  • Set the number of classes to 4 and we'll keep the quantification method as 'Equal Interval'

Quantification Methods

screen shot 2016-11-17 at 10 59 52 am

You can also read more about various tools in Carto in our new Learn!

Our final map uses the red ramp for Republican Party and a Blue ramp for Democratic party. Let's go to the CartoCSS editor to see what has been populated based on our styling choices.

  • In Builder, we are using TurboCarto a CartoCSS preprocessor
  • polygon-fill is where our classification is happening
  • In order to symbolize Democratic and Republican(referred as 'dem' and 'gop' in our dataset) using two different ramps, we'll do the following:
#layer {

  line-width: 1;
  line-color: #FFF;
  line-opacity: 0.5;

  [county_winner = 'dem'] {
  polygon-fill: ramp([pct_dem], (#CCFDFE, #6193C7, #9CC0E3, #006AAB), equal);
  }
  [county_winner = 'gop'] {
   polygon-fill: ramp([pct_gop], (#FBD0D0, #E99D99, #CF615D, #B02029), equal);
  }
}
  • Click Apply to see the map update

Ok! So now we have a map that shows which counties the Democrats won and which counties the Republicans won. The darker the color, the higher the percentage of votes in that county.

  • We'll make a couple of more modifications to our CartoCSS to polish the map
  • we'll remove the line- properties because we are looking at different reporting units
  • and we'll set some of the polygon- properties as follows
#layer {
  
  polygon-gamma: 0.4;
  polygon-opacity: 0.9;

  [county_winner = 'dem'] {
  polygon-fill: ramp([pct_dem], (#CCFDFE, #6193C7, #9CC0E3, #006AAB), equal);
  }
  [county_winner = 'gop'] {
   polygon-fill: ramp([pct_gop], (#FBD0D0, #E99D99, #CF615D, #B02029), equal);
  }

  polygon-fill: #d3d3d3;
}
  • Next, we'll change the basemap to Positron Lite
  • in the Layer view, click on the bottom basemap layer and choose Positron lite
  • Next, we'll style our state boundary lines
  • in the Style tab, we'll set the line-width to 1 and the line-color to #FFFFFF
  • And finally, let's label the state points using the following settings

screen shot 2016-10-05 at 8 58 10 pm

Demographic Data Widgets

  • Download the Education Dataset

  • In order to add demographic context to this election, we have prepared a dataset from the CARTO Data Observatory that contains educational attainment for US counties. We have chosen to focus on white male and white female educational attainment for this exercise. You can grab more census variables from another data repository like the Data Observatory, Social Explorer (http://www.socialexplorer.com/), or directly from the Census if you like.

  • We want to add several widgets to our map that show total numbers of white male educational attainment. However, these are raw numbers currently. Let's divide by the total population to get percentages. For example:

ALTER TABLE education_by_race_county 
ADD COLUMN male_less_than_HS_pct numeric,
ADD COLUMN male_HS_pct numeric,
ADD COLUMN male_some_college_pct numeric,
ADD COLUMN male_bachelors_pct numeric;


UPDATE education_by_race_county
SET male_less_than_HS_pct = 100 * total_male_less_than_high_school_diploma/(total_male);
UPDATE education_by_race_county
SET male_HS_pct = 100 * total_male_high_school_graduate_includes_equivalency/(total_male);
UPDATE education_by_race_county
SET male_some_college_pct = 100 * total_male_some_college_or_associates_degree/(total_male);
UPDATE education_by_race_county
SET male_bachelors_pct = 100 * total_male_bachelors_degree_or_higher/(total_male);
  • On the election_results layer (which should now have polygons), join the education data layer. Keep all columns from both layers.

Join the county shapes layer with the election results map you just created

County Shapes Layer > Add Analysis > Join columns from 2nd layer

  • Select the election results layer as the second layer, and use foreign keys "geoid" and "fips"

  • Use geometries from the county shapes layer, and keep the name column from county shapes and all columns from election results

  • Add histogram widgets to explore and query the map by educational attainment

[Final Map] (https://michellemho.carto.com/builder/2585e4f8-ab7f-11e6-be52-0ef7f98ade21/embed)

screen shot 2016-11-17 at 11 52 06 am

You can continue to ask more questions with the election data joining it with other demographic level data and create more interesting dashboards!


Exploring more with the maps

We created the entire Dashboard for the election data in Carto. Go on, and try it too!

screen shot 2016-11-17 at 11 06 45 am

Extra Information

Diagram of CARTO's architecture

Structure of CARTO

CARTO's Data Science toolkit

Signing up

Our APIs

JavaScript Libraries

  • Carto.js -- workhorse library for building custom applications off of CARTO's platform
  • Torque.js -- create spatio-temporal maps from your time-series data

Programmatic access / R and Python workflows


Demos with JavaScript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment