Skip to content

Instantly share code, notes, and snippets.

@choct155
Last active January 1, 2016 14:39
Show Gist options
  • Save choct155/8159530 to your computer and use it in GitHub Desktop.
Save choct155/8159530 to your computer and use it in GitHub Desktop.
Just a test of interactivity for a rCharts choropleth
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Choropleth Maps with rCharts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is just a quick script expounding a bit on the example provided by [Ramnath Vaidyanathan](http://rcharts.io/viewer/?6735051#.Ur3MGN_xCak)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext rmagic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can first grab the example obesity data from Berkeley."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%R\n",
"\n",
"# read data and replace dots in names with underscores\n",
"obesity = read.csv(\n",
" 'http://www.stat.berkeley.edu/classes/s133/data/obesity.csv',\n",
" stringsAsFactors = F\n",
")\n",
"\n",
"print(head(obesity))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
" State Approach.to.Address.Childhood.Obesity State.Task.Force.Committee\n",
"1 Alabama Both Yes \n",
"2 Alaska None No \n",
"3 Arizona Both Yes \n",
"4 Arkansas Both Yes \n",
"5 California School nutrition program No \n",
"6 Colorado School nutrition program Yes \n",
" Mandates.BMI.Screening Mandates.Obesity.Treatment.Coverage\n",
"1 No No \n",
"2 No No \n",
"3 No No \n",
"4 Yes No \n",
"5 No No \n",
"6 No No \n",
" Childhood.Obesity.Rate Adult.Obesity.Rate\n",
"1 0.17 0.623\n",
"2 0.11 0.624\n",
"3 0.12 0.535\n",
"4 0.16 0.619\n",
"5 0.13 0.579\n",
"6 0.10 0.523\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For some reason that may soon be apparenty, Ramnath has chosen to replace the periods in the variable names with underscores."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%R\n",
"\n",
"names(obesity) = gsub(\"\\\\.\", \"_\", names(obesity))\n",
"\n",
"print(head(obesity))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
" State Approach_to_Address_Childhood_Obesity State_Task_Force_Committee\n",
"1 Alabama Both Yes \n",
"2 Alaska None No \n",
"3 Arizona Both Yes \n",
"4 Arkansas Both Yes \n",
"5 California School nutrition program No \n",
"6 Colorado School nutrition program Yes \n",
" Mandates_BMI_Screening Mandates_Obesity_Treatment_Coverage\n",
"1 No No \n",
"2 No No \n",
"3 No No \n",
"4 Yes No \n",
"5 No No \n",
"6 No No \n",
" Childhood_Obesity_Rate Adult_Obesity_Rate\n",
"1 0.17 0.623\n",
"2 0.11 0.624\n",
"3 0.12 0.535\n",
"4 0.16 0.619\n",
"5 0.13 0.579\n",
"6 0.10 0.523\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We also need to add abbreviated state names, presumably to sync up with the map."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%R\n",
"\n",
"library(plyr)\n",
"library(stringr)\n",
"\n",
"#Trim the white space from State and create an abbreviated version called state\n",
"obesity = plyr::mutate(obesity, \n",
" State = str_trim(State),\n",
" state = state.abb[match(State, state.name)],\n",
")\n",
"\n",
"print(head(obesity))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
" State Approach_to_Address_Childhood_Obesity State_Task_Force_Committee\n",
"1 Alabama Both Yes \n",
"2 Alaska None No \n",
"3 Arizona Both Yes \n",
"4 Arkansas Both Yes \n",
"5 California School nutrition program No \n",
"6 Colorado School nutrition program Yes \n",
" Mandates_BMI_Screening Mandates_Obesity_Treatment_Coverage\n",
"1 No No \n",
"2 No No \n",
"3 No No \n",
"4 Yes No \n",
"5 No No \n",
"6 No No \n",
" Childhood_Obesity_Rate Adult_Obesity_Rate state\n",
"1 0.17 0.623 AL\n",
"2 0.11 0.624 AK\n",
"3 0.12 0.535 AZ\n",
"4 0.16 0.619 AR\n",
"5 0.13 0.579 CA\n",
"6 0.10 0.523 CO\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A little reminder on what mutate does..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%R\n",
"library(plyr)\n",
"\n",
"print(help(mutate))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"R Help on \u2018mutate\u2019mutate package:plyr R Documentation\n",
"\n",
"_\bM_\bu_\bt_\ba_\bt_\be _\ba _\bd_\ba_\bt_\ba _\bf_\br_\ba_\bm_\be _\bb_\by _\ba_\bd_\bd_\bi_\bn_\bg _\bn_\be_\bw _\bo_\br _\br_\be_\bp_\bl_\ba_\bc_\bi_\bn_\bg _\be_\bx_\bi_\bs_\bt_\bi_\bn_\bg _\bc_\bo_\bl_\bu_\bm_\bn_\bs.\n",
"\n",
"_\bD_\be_\bs_\bc_\br_\bi_\bp_\bt_\bi_\bo_\bn:\n",
"\n",
" This function is very similar to \u2018transform\u2019 but it executes the\n",
" transformations iteratively so that later transformations can use\n",
" the columns created by earlier transformations. Like transform,\n",
" unnamed components are silently dropped.\n",
"\n",
"_\bU_\bs_\ba_\bg_\be:\n",
"\n",
" mutate(.data, ...)\n",
" \n",
"_\bA_\br_\bg_\bu_\bm_\be_\bn_\bt_\bs:\n",
"\n",
" .data: the data frame to transform\n",
"\n",
" ...: named parameters giving definitions of new columns.\n",
"\n",
"_\bD_\be_\bt_\ba_\bi_\bl_\bs:\n",
"\n",
" Mutate seems to be considerably faster than transform for large\n",
" data frames.\n",
"\n",
"_\bS_\be_\be _\bA_\bl_\bs_\bo:\n",
"\n",
" \u2018subset\u2019, \u2018summarise\u2019, \u2018arrange\u2019. For another somewhat different\n",
" approach to solving the same problem, see \u2018within\u2019.\n",
"\n",
"_\bE_\bx_\ba_\bm_\bp_\bl_\be_\bs:\n",
"\n",
" # Examples from transform\n",
" mutate(airquality, Ozone = -Ozone)\n",
" mutate(airquality, new = -Ozone, Temp = (Temp - 32) / 1.8)\n",
" \n",
" # Things transform can't do\n",
" mutate(airquality, Temp = (Temp - 32) / 1.8, OzT = Ozone / Temp)\n",
" \n",
" # mutate is rather faster than transform\n",
" system.time(transform(baseball, avg_ab = ab / g))\n",
" system.time(mutate(baseball, avg_ab = ab / g))\n",
" \n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the part I was unclear about. I see no shapefile being read in, so we need to dissect the `choropleth` method."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%R\n",
"\n",
"#Create map\n",
"map<-rCharts::choropleth(\n",
" cut(Adult_Obesity_Rate, 5, labels = F) ~ state,\n",
" data = obesity,\n",
" pal = 'PuRd'\n",
")\n",
"\n",
"#Save map to HTML\n",
"map$save('rChart_map.html',cdn=TRUE)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Define display function\n",
"def display_rchart(filename):\n",
" from IPython.display import display, HTML, Javascript\n",
" display(HTML(\"<iframe src=/files/\" + filename + \" width=850 height=430 seamless></iframe>\"))\n",
"\n",
"display_rchart('rChart_map.html')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<iframe src=https://github.com/choct155/MiscProj/blob/master/"+ filename +" width=850 height=430 seamless></iframe>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x570fa50>"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%R\n",
"\n",
"print(choropleth)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"function(x, data, pal, map = 'usa', ...){\n",
" fml = lattice::latticeParseFormula(x, data = data)\n",
" data = transform(data, fillKey = fml$left)\n",
" mypal = RColorBrewer::brewer.pal(length(unique(fml$left)), pal)\n",
" d <- Datamaps$new()\n",
" d$set(\n",
" scope = map,\n",
" fills = as.list(setNames(mypal, unique(fml$left))),\n",
" data = dlply(data, fml$right.name),\n",
" ...\n",
" )\n",
" return(d)\n",
"}\n",
"<environment: namespace:rCharts>\n"
]
}
],
"prompt_number": 21
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment