Skip to content

Instantly share code, notes, and snippets.

@mehak-sachdeva
Last active October 14, 2016 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehak-sachdeva/e030dca4c2ee333423f7e7d1fb1090ea to your computer and use it in GitHub Desktop.
Save mehak-sachdeva/e030dca4c2ee333423f7e7d1fb1090ea to your computer and use it in GitHub Desktop.

##URL for the gist : http://bit.ly/2dxJxUM

##Data Observatory: We will query the real-estate data for Los Angeles, CA. We start with an empty map and rename the table to LA:

INSERT INTO LA(the_geom, name)
SELECT *
FROM OBS_GetBoundariesByPointAndRadius(
  CDB_LatLng(34.0522,-118.2437),
  10000 * 1.609, 
  'us.census.tiger.census_tract_clipped'
) As m(the_geom, geoid)
WHERE geoid LIKE '06037%' 

1000 * 1.609 defining a 10 mile radius, 10 km converting to miles us.census.tiger.census_tract_clipped using the census tract BoundaryID 06037% Los Angeles County, CA FIPS - 06-037

we will search for measures within the Data Observatory that are to do with real-estate prices or the source as 'zillow'

SELECT * 
FROM OBS_Search('gini')

Copy the id of the index you want. In our case, gini corresponds to us.census.acs.B19083001 Getting the measure:

UPDATE untitled_table_9
SET real_estate = OBS_GetMeasure(ST_PointOnSurface(the_geom), 'us.census.acs.B19083001','area','us.census.tiger.census_tract')

Style your data according to the new columns you just populated and explore the many more measure and boundary functions with the CARTO Data Observatory!

Link to the .carto file here: https://team.carto.com/u/mehak-carto/builder/e23647d0-8ca0-11e6-af09-0ee66e2c9693/embed


Clinton v Sanders Primary Choropleth

Download the .carto file for the map to follow-along here:

https://team.carto.com/u/mehak-carto/viz/dcb30110-68f4-4e0c-a1f5-246a467abeee/public_map

screen shot 2016-10-05 at 11 45 26 am

Welcome to CARTO Builder!

Data

For this map, we are using the same data as the NY Times map. 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:

Attributes

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

  • To follow the NY Times inspiration map, we calculated the margin of victory for each candidate as well as the democratic or republican winner for each reporting unit
  • For this map we will symbolize two variables
  • The democratic winner for each county (d_winner)
  • And using a color ramp, the margin of each candidate's victory (d_margin_pc)

Choropleth Styling

  • Let's start by styling our choropleth map
  • Click on the Style tab
  • Style by value
  • Search for d_margin_pc
  • Set the number of classes to 4 and we'll keep the quantiles classification method

Our final map uses the orange ramp for Sanders and a teal ramp for Clinton. 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 Clinton and Sanders using two different ramps, we'll do something similar to what we did with the choropleth map of Obama v Romney
#layer {

  line-width: 1;
  line-color: #FFF;
  line-opacity: 0.5;
  
  [d_winner = 'Clinton'] {
      polygon-fill: ramp([d_margin_pc], (#d1eeea, #85c4c9, #4f90a6, #2a5674), quantiles);
  }
  
  [d_winner = 'Sanders'] {
      polygon-fill: ramp([d_margin_pc], (#fde0c5, #f8b58b, #f2855d, #eb4a40), quantiles);
  }
}
  • Click Apply to see the map update

Ok! So now we have a map that shows which counties Clinton won and which counties Sanders won. The darker the color, the higher the margin of their victory 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;
  
  
  [d_winner = 'Clinton'] {
      polygon-fill: ramp([d_margin_pc], (#d1eeea, #85c4c9, #4f90a6, #2a5674), quantiles);
  }
  
  [d_winner = 'Sanders'] {
      polygon-fill: ramp([d_margin_pc], (#fde0c5, #f8b58b, #f2855d, #eb4a40), quantiles);
  }
  
  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

Widgets

Ok! So now we have our choropleth map styled. This map is great, but there are so many other attributes in the data that would be interesting to explore and filter in relation to these results.

  • Click on the Layer as check the option "Add as Widget" for frac_clinton,frac_sanders,d_winner, and state_name
  • Next, we'll rename each widget
  • And reorder them

Let's take a look at what we can do with the Widgets

  • We can filter
  • We can style the map based on that attribute
  • by category
  • by quantity -> data distribution and color ramp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment