{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from lets_plot import *\n",
"from lets_plot.mapping import *\n",
"\n",
"LetsPlot.setup_html() "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"blank_theme = theme(line=element_blank(), axis=element_blank())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"w,h = 400,250\n",
"\n",
"tooltip_content = (\n",
" layer_tooltips()\n",
" .line('count|@{..count..} (@{..prop..})')\n",
" .line('total|@{..sum..}')\n",
" .format('..prop..', '.0%')\n",
" .format('..count..', '.1f')\n",
" .format('..sum..', '.1f')\n",
")\n",
"\n",
"def bar_and_pie(bar_plot, pie_plot):\n",
" bunch = GGBunch()\n",
" bunch.add_plot(bar_plot, 0, 0)\n",
" bunch.add_plot(pie_plot + blank_theme, w, 0)\n",
" return bunch.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"df = {\n",
" 'name': ['a', 'b', 'c', 'd', 'b'],\n",
" 'value': [40, 90, 10, 50, 20 ],\n",
"}\n",
"\n",
"p = ggplot(df) + ggsize(w,h)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# data as is - stat identity\n",
"\n",
"bar_and_pie(\n",
" p + geom_bar(aes(x='name', y='value', fill='name'), stat='identity'),\n",
" p + geom_pie(aes(slice='value', fill='name'), size=10, stat='identity', stroke=1)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# count \n",
"\n",
"bar_and_pie(\n",
" p + geom_bar(aes(x='name', fill='name')),\n",
" p + geom_pie(aes(fill='name'), size=10, stroke=1, tooltips=tooltip_content)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# sum\n",
"\n",
"bar_and_pie(\n",
" p + geom_bar(aes(x='name', fill='name', weight='value')),\n",
" p + geom_pie(aes(fill='name', weight='value'), stroke=1, size=10, tooltips=tooltip_content)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# sum + ordering\n",
"\n",
"bar_and_pie(\n",
" p + geom_bar(aes(x='name', fill=as_discrete('name', order_by='..count..'), weight='value')),\n",
" p + geom_pie(\n",
" aes(fill=as_discrete('name', order_by='..count..'), weight='value'),\n",
" stroke=1, size=10,\n",
" tooltips=tooltip_content\n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2 = {\n",
" 'x': [1, 1, 1, 1, 1, 1.5, 1.5, 2, 2, 2 ],\n",
" 'y': [1, 1, 1, 1, 1, 2, 2, 1.5, 1.5, 1.5],\n",
" 's': [3, 1, 2, 1, 4, 1, 3, 3, 3, 1],\n",
" 'n': ['a', 'b', 'a', 'c', 'a', 'a', 'b', 'c', 'a', 'b']\n",
"}\n",
"\n",
"p2 = ggplot(df2) + xlim(0.5,2.5) + ylim(0.5,2.5)\n",
" \n",
"p2 + geom_pie(aes('x', 'y', slice='s', fill='n'), size=10, hole=0.3, stat='identity') "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p2 + geom_pie(aes('x', 'y', fill=as_discrete('n', order_by='..count..', order=-1), weight='s'), \n",
" size=10, hole=0.3) "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Map total count in each point to size\n",
"\n",
"p2 + geom_pie(aes('x', 'y', fill=as_discrete('n', order_by='..count..', order=-1), weight='s', size='..sum..'),\n",
" hole=0.3, tooltips=tooltip_content) "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mapping 'fill' and 'size' to the same variable\n",
"\n",
"ggplot({'n': [\"a\", \"b\", \"c\"], 's': [1, 2, 3]}) + blank_theme + \\\n",
" geom_pie(aes(fill='n', slice='s', size='n'), stat=\"identity\") \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Unnamed: 0 | \n",
" manufacturer | \n",
" model | \n",
" displ | \n",
" year | \n",
" cyl | \n",
" trans | \n",
" drv | \n",
" cty | \n",
" hwy | \n",
" fl | \n",
" class | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" audi | \n",
" a4 | \n",
" 1.8 | \n",
" 1999 | \n",
" 4 | \n",
" auto(l5) | \n",
" f | \n",
" 18 | \n",
" 29 | \n",
" p | \n",
" compact | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" audi | \n",
" a4 | \n",
" 1.8 | \n",
" 1999 | \n",
" 4 | \n",
" manual(m5) | \n",
" f | \n",
" 21 | \n",
" 29 | \n",
" p | \n",
" compact | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" audi | \n",
" a4 | \n",
" 2.0 | \n",
" 2008 | \n",
" 4 | \n",
" manual(m6) | \n",
" f | \n",
" 20 | \n",
" 31 | \n",
" p | \n",
" compact | \n",
"
\n",
" \n",
" 3 | \n",
" 4 | \n",
" audi | \n",
" a4 | \n",
" 2.0 | \n",
" 2008 | \n",
" 4 | \n",
" auto(av) | \n",
" f | \n",
" 21 | \n",
" 30 | \n",
" p | \n",
" compact | \n",
"
\n",
" \n",
" 4 | \n",
" 5 | \n",
" audi | \n",
" a4 | \n",
" 2.8 | \n",
" 1999 | \n",
" 6 | \n",
" auto(l5) | \n",
" f | \n",
" 16 | \n",
" 26 | \n",
" p | \n",
" compact | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n",
"0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n",
"1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n",
"2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n",
"3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n",
"4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n",
"\n",
" fl class \n",
"0 p compact \n",
"1 p compact \n",
"2 p compact \n",
"3 p compact \n",
"4 p compact "
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"mpg_df = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n",
"mpg_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"p3 = ggplot(mpg_df) + ggsize(400,300)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ordered_class = as_discrete('class', order_by='..count..')\n",
"\n",
"bar_and_pie(\n",
" p3 + geom_bar(aes(x='class', fill=ordered_class)), \n",
" p3 + geom_pie(aes(fill=ordered_class), size=10, hole=0.3)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"bar_and_pie(\n",
" p3 + geom_bar(aes(x='class', fill=ordered_class, weight ='displ')), \n",
" p3 + geom_pie(aes(fill=ordered_class, weight='displ'), size=10, hole=0.3, tooltips=tooltip_content)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(mpg_df) + blank_theme + \\\n",
" geom_pie(aes(fill=ordered_class, weight='displ', size='..sum..'), hole=0.3, tooltips=tooltip_content) + \\\n",
" facet_grid(x='year') + \\\n",
" scale_size(range=[5, 10]) + \\\n",
" guides(size='none') "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(mpg_df) + blank_theme + \\\n",
" geom_pie(aes(fill=as_discrete('drv', order=1), size='..sum..'), tooltips=tooltip_content) + \\\n",
" facet_wrap(facets='trans', ncol=5) + \\\n",
" scale_size(range=[2, 10]) + \\\n",
" guides(size='none') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"length = {\n",
" 'name' : ['20-50 km', '50-75 km', '10-20 km', '75-100 km', '3-5 km', '7-10 km', '5-7 km', '>100 km', '2-3 km'],\n",
" 'count': [1109, 696, 353, 192, 168, 86, 74, 65, 53],\n",
" 'explode': [0, 0, 0, 0.1, 0.1, 0.2, 0.3, 0.4, 0.6]\n",
"}\n",
"\n",
"ggplot(length) + blank_theme + \\\n",
" geom_pie(aes(fill='name', slice='count', explode='explode'), stat='identity',\n",
" stroke=1, stroke_color='black', size=20) + \\\n",
" scale_fill_gradient(low='dark_blue', high='light_green') "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calories = {\n",
" 'slice': [35, 25, 25, 15],\n",
" 'label': [\"Apples\", \"Bananas\", \"Cherries\", \"Dates\"],\n",
" 'explode': [0.1, 0, 0, 0]\n",
"}\n",
"\n",
"p4 = ggplot(calories) + \\\n",
" blank_theme + \\\n",
" scale_fill_brewer(palette='Set1') + \\\n",
" ggsize(w, h)\n",
"\n",
"p_pie = p4 + \\\n",
" geom_pie(aes(fill='label', slice='slice', explode='explode'), stat='identity', size=18)\n",
"\n",
"p_donut = p4 + \\\n",
" geom_pie(aes(fill='label', slice='slice', explode='explode'), stat='identity', hole=0.8, size=18)\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p_pie + theme(legend_position='none'), 0, 0)\n",
"bunch.add_plot(p_donut, w, 0)\n",
"bunch"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Municipality | \n",
" Yes | \n",
" Yes% | \n",
" No | \n",
" No% | \n",
" Registered | \n",
" Voted | \n",
" Voted% | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Andrijevica | \n",
" 1084 | \n",
" 27.60 | \n",
" 2824 | \n",
" 71.89 | \n",
" 4369 | \n",
" 3928 | \n",
" 89.91 | \n",
"
\n",
" \n",
" 1 | \n",
" Bar | \n",
" 16640 | \n",
" 63.07 | \n",
" 9496 | \n",
" 35.99 | \n",
" 32255 | \n",
" 26382 | \n",
" 81.79 | \n",
"
\n",
" \n",
" 2 | \n",
" Berane | \n",
" 11268 | \n",
" 46.85 | \n",
" 12618 | \n",
" 52.46 | \n",
" 28342 | \n",
" 24051 | \n",
" 84.86 | \n",
"
\n",
" \n",
" 3 | \n",
" Bijelo Polje | \n",
" 19405 | \n",
" 55.36 | \n",
" 15437 | \n",
" 44.04 | \n",
" 40110 | \n",
" 35051 | \n",
" 87.39 | \n",
"
\n",
" \n",
" 4 | \n",
" Budva | \n",
" 5908 | \n",
" 52.75 | \n",
" 5180 | \n",
" 46.25 | \n",
" 12797 | \n",
" 11200 | \n",
" 87.52 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Municipality Yes Yes% No No% Registered Voted Voted%\n",
"0 Andrijevica 1084 27.60 2824 71.89 4369 3928 89.91\n",
"1 Bar 16640 63.07 9496 35.99 32255 26382 81.79\n",
"2 Berane 11268 46.85 12618 52.46 28342 24051 84.86\n",
"3 Bijelo Polje 19405 55.36 15437 44.04 40110 35051 87.39\n",
"4 Budva 5908 52.75 5180 46.25 12797 11200 87.52"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"referendum_df = pd.read_csv(\"data/montenegrin_referendum_2006.csv\")\n",
"referendum_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/olarionova/miniconda3/lib/python3.7/site-packages/geopandas/array.py:93: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the number of parts of a multi-part geometry.\n",
" aout[:] = out\n",
"/Users/olarionova/miniconda3/lib/python3.7/site-packages/geopandas/array.py:93: ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.\n",
" aout[:] = out\n"
]
}
],
"source": [
"from lets_plot.geo_data import *\n",
"\n",
"country = 'Montenegro'\n",
"geocoder = geocode_states(names=referendum_df['Municipality']).scope(country)\n",
"\n",
"boundaries = geocoder.get_boundaries(resolution=15)\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" found name | \n",
" geometry | \n",
" Municipality | \n",
" Yes% | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Andrijevica | \n",
" Opština Andrijevica | \n",
" MULTIPOLYGON (((19.61901 42.80456, 19.63347 42... | \n",
" Andrijevica | \n",
" 27.60 | \n",
"
\n",
" \n",
" 1 | \n",
" Bar | \n",
" Opština Bar | \n",
" MULTIPOLYGON (((18.98948 42.16385, 18.98910 42... | \n",
" Bar | \n",
" 63.07 | \n",
"
\n",
" \n",
" 2 | \n",
" Berane | \n",
" Opština Berane | \n",
" MULTIPOLYGON (((19.61729 42.88021, 19.62012 42... | \n",
" Berane | \n",
" 46.85 | \n",
"
\n",
" \n",
" 3 | \n",
" Bijelo Polje | \n",
" Opština Bijelo Polje | \n",
" MULTIPOLYGON (((19.41061 43.07992, 19.41992 43... | \n",
" Bijelo Polje | \n",
" 55.36 | \n",
"
\n",
" \n",
" 4 | \n",
" Budva | \n",
" Opština Budva | \n",
" MULTIPOLYGON (((18.80014 42.28039, 18.79997 42... | \n",
" Budva | \n",
" 52.75 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state found name \\\n",
"0 Andrijevica Opština Andrijevica \n",
"1 Bar Opština Bar \n",
"2 Berane Opština Berane \n",
"3 Bijelo Polje Opština Bijelo Polje \n",
"4 Budva Opština Budva \n",
"\n",
" geometry Municipality Yes% \n",
"0 MULTIPOLYGON (((19.61901 42.80456, 19.63347 42... Andrijevica 27.60 \n",
"1 MULTIPOLYGON (((18.98948 42.16385, 18.98910 42... Bar 63.07 \n",
"2 MULTIPOLYGON (((19.61729 42.88021, 19.62012 42... Berane 46.85 \n",
"3 MULTIPOLYGON (((19.41061 43.07992, 19.41992 43... Bijelo Polje 55.36 \n",
"4 MULTIPOLYGON (((18.80014 42.28039, 18.79997 42... Budva 52.75 "
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"boundaries = pd.merge(boundaries, referendum_df[['Municipality','Yes%']], left_on='state', right_on='Municipality')\n",
"boundaries.head()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot_title = ggtitle(\"Results of the Montenegrin independence referendum, 2006\")\n",
"\n",
"fill_colors = ['#a50026', '#d73027', '#66bd63', '#4daf4a','#006837']\n",
" \n",
"map_layer = geom_map(aes(fill=\"Yes%\"), \n",
" data=boundaries,\n",
" tooltips=layer_tooltips()\n",
" .title('@state')\n",
" .line('\\'Yes\\' votes|@{Yes%}')\n",
" .format('Yes%', '{} %')) + \\\n",
" scale_fill_gradientn(fill_colors)\n",
"\n",
"ggplot() + map_layer + plot_title + blank_theme + ggsize(800, 700) "
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Municipality | \n",
" Registered | \n",
" Vote | \n",
" Number | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Andrijevica | \n",
" 4369 | \n",
" No | \n",
" 2824 | \n",
"
\n",
" \n",
" 1 | \n",
" Bar | \n",
" 32255 | \n",
" No | \n",
" 9496 | \n",
"
\n",
" \n",
" 2 | \n",
" Berane | \n",
" 28342 | \n",
" No | \n",
" 12618 | \n",
"
\n",
" \n",
" 3 | \n",
" Bijelo Polje | \n",
" 40110 | \n",
" No | \n",
" 15437 | \n",
"
\n",
" \n",
" 4 | \n",
" Budva | \n",
" 12797 | \n",
" No | \n",
" 5180 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 58 | \n",
" Rožaje | \n",
" 19646 | \n",
" Blank | \n",
" 90 | \n",
"
\n",
" \n",
" 59 | \n",
" Šavnik | \n",
" 2306 | \n",
" Blank | \n",
" 20 | \n",
"
\n",
" \n",
" 60 | \n",
" Tivat | \n",
" 10776 | \n",
" Blank | \n",
" 91 | \n",
"
\n",
" \n",
" 61 | \n",
" Ulcinj | \n",
" 17117 | \n",
" Blank | \n",
" 137 | \n",
"
\n",
" \n",
" 62 | \n",
" Žabljak | \n",
" 3407 | \n",
" Blank | \n",
" 24 | \n",
"
\n",
" \n",
"
\n",
"
63 rows × 4 columns
\n",
"
"
],
"text/plain": [
" Municipality Registered Vote Number\n",
"0 Andrijevica 4369 No 2824\n",
"1 Bar 32255 No 9496\n",
"2 Berane 28342 No 12618\n",
"3 Bijelo Polje 40110 No 15437\n",
"4 Budva 12797 No 5180\n",
".. ... ... ... ...\n",
"58 Rožaje 19646 Blank 90\n",
"59 Šavnik 2306 Blank 20\n",
"60 Tivat 10776 Blank 91\n",
"61 Ulcinj 17117 Blank 137\n",
"62 Žabljak 3407 Blank 24\n",
"\n",
"[63 rows x 4 columns]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mref_df = referendum_df\n",
"\n",
"# Blank or invalid votes:\n",
"mref_df[\"Blank\"] = mref_df[\"Voted\"] - mref_df[\"Yes\"] - mref_df[\"No\"]\n",
"\n",
"mref_df = mref_df[[\"Municipality\", \"Registered\", \"No\", \"Yes\", \"Blank\"]]\n",
"\n",
"id_vars = [\"Municipality\", \"Registered\"]\n",
"mref_df = pd.melt(frame=mref_df, id_vars=id_vars, var_name=\"Vote\", value_name=\"Number\")\n",
"mref_df"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot() + \\\n",
" map_layer + \\\n",
" geom_pie(aes(color='Vote', slice='Number'), \n",
" data=mref_df, stat='identity', \n",
" map=geocoder, map_join=['Municipality','state'],\n",
" fill_by='color', size=5, stroke=1.5) + \\\n",
" scale_color_manual(values=[ \"#e41a1c\",\"#4daf4a\", \"#999999\"]) + \\\n",
" plot_title + \\\n",
" blank_theme + \\\n",
" ggsize(800, 700)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pie_layer = geom_pie(aes('Municipality', color='Vote', weight='Number', size='..sum..', group='Municipality'),\n",
" data=mref_df,\n",
" map=geocoder, map_join=['Municipality','state'],\n",
" fill_by='color', hole=0.2, stroke=1.5,\n",
" tooltips=layer_tooltips()\n",
" .title('@Municipality')\n",
" .line('Vote|@Vote')\n",
" .line('Number|@{..count..}')\n",
" .line('Percent|@{..prop..}')\n",
" .line('Total voted|@{..sum..}')\n",
" .line('Registered|@Registered')\n",
" .format('..prop..', '.2%')\n",
" .format('Registered', ',d')) + \\\n",
" scale_color_manual(values=['#e41a1c','#4daf4a', '#999999']) + \\\n",
" scale_size(name='Total voted', range=[3, 8])\n",
"\n",
"ggplot() + \\\n",
" map_layer + \\\n",
" pie_layer + \\\n",
" plot_title + \\\n",
" blank_theme + \\\n",
" ggtitle(\"Results of the Montenegrin independence referendum, 2006\") + \\\n",
" ggsize(800, 700)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# on livemap\n",
"\n",
"ggplot() + \\\n",
" geom_livemap() + \\\n",
" map_layer + \\\n",
" pie_layer + \\\n",
" plot_title + \\\n",
" theme(legend_position='none')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}