##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
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
Welcome to CARTO Builder!
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:
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
)
- 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
to1
and theline-color
to#FFFFFF
- And finally, let's label the state points using the following settings
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
, andstate_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