{"cells":[{"cell_type":"markdown","id":"404a9199-f54f-48f2-a984-d06c6b08ac42","metadata":{"id":"404a9199-f54f-48f2-a984-d06c6b08ac42"},"source":["\"Open\n","\n","# Interactive graphics\n","\n","Javascript graphics allows one to put data oriented graphics into web documents (like this book), apps and other reproducible research documents. As mentioned, several well developed APIs have been developed to use Python, R ... as the base language where graphics are output as javascript. Here, we'll go through some examples using [plotly](https://plotly.com/), both because it's a nice library of graphics functions, but also it's what I know sort of well. However, if there's another graphics platform you like, likely there's a python and/or R API written for it.\n","\n","## Using plotly\n","Consider a dataset that has regional volumes for 20 subjects in a long dataset. I wrote some R code for reading in this dataset which you can follow along [here](https://github.com/bcaffo/MRIcloudTutorial/blob/gh-pages/ggplot/ggplot.Rmd)."]},{"cell_type":"code","execution_count":null,"id":"97121be8-2fee-4370-bf28-1c0ff381f800","metadata":{"id":"97121be8-2fee-4370-bf28-1c0ff381f800"},"outputs":[],"source":["import pandas as pd\n","import plotly.express as px\n","import numpy as np"]},{"cell_type":"code","execution_count":null,"id":"0f415fa0-4468-4b13-9e9a-e6eb60bef403","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"0f415fa0-4468-4b13-9e9a-e6eb60bef403","executionInfo":{"status":"ok","timestamp":1713275019002,"user_tz":240,"elapsed":326,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"c34b91be-17fc-42b9-822c-f4bad1e4b29a"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" id roi volume\n","0 127 Telencephalon_L 531111\n","1 127 Telencephalon_R 543404\n","2 127 Diencephalon_L 9683\n","3 127 Diencephalon_R 9678\n","4 127 Mesencephalon 10268"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
idroivolume
0127Telencephalon_L531111
1127Telencephalon_R543404
2127Diencephalon_L9683
3127Diencephalon_R9678
4127Mesencephalon10268
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"dat","summary":"{\n \"name\": \"dat\",\n \"rows\": 160,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 267,\n \"min\": 127,\n \"max\": 959,\n \"num_unique_values\": 20,\n \"samples\": [\n 127,\n 916,\n 906\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"roi\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 8,\n \"samples\": [\n \"Telencephalon_R\",\n \"Metencephalon\",\n \"Telencephalon_L\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"volume\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 207095,\n \"min\": 3214,\n \"max\": 572435,\n \"num_unique_values\": 158,\n \"samples\": [\n 495646,\n 150771,\n 91674\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":15}],"source":["dat = pd.read_csv(\"https://raw.githubusercontent.com/smart-stats/ds4bio_book/main/book/assetts/kirby21.csv\").drop(['Unnamed: 0'], axis = 1)\n","dat.head()"]},{"cell_type":"markdown","id":"aa062e76-3e3a-420b-ac5c-94aec8fc0193","metadata":{"id":"aa062e76-3e3a-420b-ac5c-94aec8fc0193"},"source":["Let's vew individual subjects. The id variable is a numeric variable, so let's create a string version."]},{"cell_type":"code","execution_count":null,"id":"0e1fac28-b0b3-4728-aa2c-13f168190c9d","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"0e1fac28-b0b3-4728-aa2c-13f168190c9d","executionInfo":{"status":"ok","timestamp":1713275019003,"user_tz":240,"elapsed":32,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"7cd44285-beaa-40d6-a392-00510b9ede3a"},"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}],"source":["dat = dat.assign(id_char = dat.id.astype(str))\n","fig = px.bar(dat, x = \"id_char\", y = \"volume\", color = \"roi\")\n","fig.show()"]},{"cell_type":"markdown","id":"01536d9b-bcf5-4585-9609-f8540249fdee","metadata":{"id":"01536d9b-bcf5-4585-9609-f8540249fdee"},"source":["Let's add an intra-cranial volume column by grouping by id, summing all volumes, then merging that data back into the main data. We'll then add a composition variable, which is the regional volumes divided by the intra-cranial volume."]},{"cell_type":"code","execution_count":null,"id":"87c5bc92-9167-49a3-ad55-9a8097113809","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"87c5bc92-9167-49a3-ad55-9a8097113809","executionInfo":{"status":"ok","timestamp":1713275019003,"user_tz":240,"elapsed":30,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"65218477-6d10-4662-c312-4f5c90d9a62e"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" id roi volume id_char icv comp\n","0 127 Telencephalon_L 531111 127 1378295 0.385339\n","1 127 Telencephalon_R 543404 127 1378295 0.394258\n","2 127 Diencephalon_L 9683 127 1378295 0.007025\n","3 127 Diencephalon_R 9678 127 1378295 0.007022\n","4 127 Mesencephalon 10268 127 1378295 0.007450"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
idroivolumeid_charicvcomp
0127Telencephalon_L53111112713782950.385339
1127Telencephalon_R54340412713782950.394258
2127Diencephalon_L968312713782950.007025
3127Diencephalon_R967812713782950.007022
4127Mesencephalon1026812713782950.007450
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"dat","summary":"{\n \"name\": \"dat\",\n \"rows\": 160,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 267,\n \"min\": 127,\n \"max\": 959,\n \"num_unique_values\": 20,\n \"samples\": [\n 127,\n 916,\n 906\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"roi\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 8,\n \"samples\": [\n \"Telencephalon_R\",\n \"Metencephalon\",\n \"Telencephalon_L\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"volume\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 207095,\n \"min\": 3214,\n \"max\": 572435,\n \"num_unique_values\": 158,\n \"samples\": [\n 495646,\n 150771,\n 91674\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"id_char\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"127\",\n \"916\",\n \"906\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"icv\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 112198,\n \"min\": 944692,\n \"max\": 1422450,\n \"num_unique_values\": 20,\n \"samples\": [\n 1378295,\n 1422450,\n 1195015\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"comp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.1607871281503432,\n \"min\": 0.003040528665330943,\n \"max\": 0.417673224651994,\n \"num_unique_values\": 160,\n \"samples\": [\n 0.4019039387180338,\n 0.008914311719870366,\n 0.12517487433653204\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":17}],"source":["icv = dat.groupby(['id']).volume.sum().reset_index().rename(columns = {'volume' : 'icv'})\n","dat = pd.merge(dat, icv, on = 'id')\n","dat = dat.assign(comp = dat.volume / dat.icv)\n","dat.head()"]},{"cell_type":"markdown","id":"a87067ec-d139-4c73-b188-0260ad274e94","metadata":{"id":"a87067ec-d139-4c73-b188-0260ad274e94"},"source":["Let's now replot our compositional data (but now normalized to have height 1)."]},{"cell_type":"code","execution_count":null,"id":"3a41392d-67f0-4d6e-a41d-121df8735e6d","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"3a41392d-67f0-4d6e-a41d-121df8735e6d","executionInfo":{"status":"ok","timestamp":1713275019004,"user_tz":240,"elapsed":29,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"55ddab87-aeaa-4cbc-a4a7-db2e2a93a130"},"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}],"source":["fig = px.bar(dat, x = \"id_char\", y = \"comp\", color = \"roi\")\n","fig.show()"]},{"cell_type":"markdown","id":"1c8cfae5-4872-48bb-b8b9-5d20face8062","metadata":{"id":"1c8cfae5-4872-48bb-b8b9-5d20face8062"},"source":["Let's look at the subject level means. Therefore, we have to average across id."]},{"cell_type":"code","execution_count":null,"id":"0948fb6b-4788-4167-8365-d7726c04a978","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":542},"id":"0948fb6b-4788-4167-8365-d7726c04a978","executionInfo":{"status":"ok","timestamp":1713275019004,"user_tz":240,"elapsed":27,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"32f8aa7b-39a6-4639-d4b0-4b08977c9aff"},"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}],"source":["roi_mean = dat.drop([\"id\", \"id_char\", \"icv\"], axis = 1).groupby([\"roi\"]).mean().reset_index()\n","fig = px.bar(roi_mean, x = \"roi\", y = \"comp\")\n","fig.show()"]},{"cell_type":"markdown","id":"c100060c-063c-4758-a848-cad9500398a4","metadata":{"id":"c100060c-063c-4758-a848-cad9500398a4"},"source":["There's a hierarchy of regions in this dataset. Let's visualize a subject's type 1 level 5 data as it exists in the hierarchy."]},{"cell_type":"code","execution_count":null,"id":"263fb6f3-6406-424c-9a4a-8fb99a50dae3","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"263fb6f3-6406-424c-9a4a-8fb99a50dae3","executionInfo":{"status":"ok","timestamp":1713275019323,"user_tz":240,"elapsed":343,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"42287218-d5b2-4527-dcc2-948310965621"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" roi level4 level3 level2 level1\n","0 SFG_L SFG_L Frontal_L CerebralCortex_L Telencephalon_L\n","1 SFG_R SFG_R Frontal_R CerebralCortex_R Telencephalon_R\n","2 SFG_PFC_L SFG_L Frontal_L CerebralCortex_L Telencephalon_L\n","3 SFG_PFC_R SFG_R Frontal_R CerebralCortex_R Telencephalon_R\n","4 SFG_pole_L SFG_L Frontal_L CerebralCortex_L Telencephalon_L"],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
roilevel4level3level2level1
0SFG_LSFG_LFrontal_LCerebralCortex_LTelencephalon_L
1SFG_RSFG_RFrontal_RCerebralCortex_RTelencephalon_R
2SFG_PFC_LSFG_LFrontal_LCerebralCortex_LTelencephalon_L
3SFG_PFC_RSFG_RFrontal_RCerebralCortex_RTelencephalon_R
4SFG_pole_LSFG_LFrontal_LCerebralCortex_LTelencephalon_L
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"multilevel_lookup","summary":"{\n \"name\": \"multilevel_lookup\",\n \"rows\": 283,\n \"fields\": [\n {\n \"column\": \"roi\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 281,\n \"samples\": [\n \"PrCu_R\",\n \"Mammillary_R\",\n \"MCP_cb_L\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level4\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 139,\n \"samples\": [\n \"TempSul_R\",\n \"Medulla_L\",\n \"FuG_R\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level3\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 55,\n \"samples\": [\n \"CorpusCallosum_R\",\n \"Temporal_R\",\n \"InferiorWM_L\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level2\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"CerebralCortex_L\",\n \"Sulcus_L\",\n \"WhiteMatter_R\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level1\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 8,\n \"samples\": [\n \"Telencephalon_R\",\n \"Metencephalon\",\n \"Telencephalon_L\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":20}],"source":["## load in the hierarchy information\n","url = \"https://raw.githubusercontent.com/bcaffo/MRIcloudT1volumetrics/master/inst/extdata/multilevel_lookup_table.txt\"\n","multilevel_lookup = pd.read_csv(url, sep = \"\\t\").drop(['Level5'], axis = 1)\n","multilevel_lookup = multilevel_lookup.rename(columns = {\n"," \"modify\" : \"roi\",\n"," \"modify.1\" : \"level4\",\n"," \"modify.2\" : \"level3\",\n"," \"modify.3\" : \"level2\",\n"," \"modify.4\" : \"level1\"})\n","multilevel_lookup = multilevel_lookup[['roi', 'level4', 'level3', 'level2', 'level1']]\n","multilevel_lookup.head()"]},{"cell_type":"code","execution_count":null,"id":"983a75a6-8b65-46b7-9f2e-3c271c84131c","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":206},"id":"983a75a6-8b65-46b7-9f2e-3c271c84131c","executionInfo":{"status":"ok","timestamp":1713275019699,"user_tz":240,"elapsed":389,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"d49b564a-83aa-4764-d449-46e3fc1ce249"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[" roi volume level4 level3 level2 level1 \\\n","0 SFG_L 12926 SFG_L Frontal_L CerebralCortex_L Telencephalon_L \n","1 SFG_R 10050 SFG_R Frontal_R CerebralCortex_R Telencephalon_R \n","2 SFG_PFC_L 12783 SFG_L Frontal_L CerebralCortex_L Telencephalon_L \n","3 SFG_PFC_R 11507 SFG_R Frontal_R CerebralCortex_R Telencephalon_R \n","4 SFG_pole_L 3078 SFG_L Frontal_L CerebralCortex_L Telencephalon_L \n","\n"," icv comp \n","0 ICV 0.009350 \n","1 ICV 0.007270 \n","2 ICV 0.009247 \n","3 ICV 0.008324 \n","4 ICV 0.002227 "],"text/html":["\n","
\n","
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
roivolumelevel4level3level2level1icvcomp
0SFG_L12926SFG_LFrontal_LCerebralCortex_LTelencephalon_LICV0.009350
1SFG_R10050SFG_RFrontal_RCerebralCortex_RTelencephalon_RICV0.007270
2SFG_PFC_L12783SFG_LFrontal_LCerebralCortex_LTelencephalon_LICV0.009247
3SFG_PFC_R11507SFG_RFrontal_RCerebralCortex_RTelencephalon_RICV0.008324
4SFG_pole_L3078SFG_LFrontal_LCerebralCortex_LTelencephalon_LICV0.002227
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"subjectData","summary":"{\n \"name\": \"subjectData\",\n \"rows\": 280,\n \"fields\": [\n {\n \"column\": \"roi\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 274,\n \"samples\": [\n \"AG_L\",\n \"LV_Inferior_L\",\n \"MFOWM_R\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"volume\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 6212,\n \"min\": 2,\n \"max\": 59435,\n \"num_unique_values\": 269,\n \"samples\": [\n 7430,\n 1511,\n 4400\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level4\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 136,\n \"samples\": [\n \"GCC_R\",\n \"Insula_R\",\n \"midbrain_L\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level3\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 54,\n \"samples\": [\n \"midbrain_R\",\n \"OcciptSul_L\",\n \"CinguSul_R\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level2\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"CerebralCortex_L\",\n \"Sulcus_L\",\n \"WhiteMatter_R\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"level1\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 8,\n \"samples\": [\n \"Telencephalon_R\",\n \"Metencephalon\",\n \"Telencephalon_L\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"icv\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"ICV\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"comp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.004493702718252288,\n \"min\": 1.4467404214933544e-06,\n \"max\": 0.04299350847572876,\n \"num_unique_values\": 269,\n \"samples\": [\n 0.005374640665847812\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":21}],"source":["## Now load in the subject data\n","id = 127\n","subjectData = pd.read_csv(\"https://raw.githubusercontent.com/smart-stats/ds4bio_book/main/book/assetts/kirby21AllLevels.csv\")\n","subjectData = subjectData.loc[(subjectData.type == 1) & (subjectData.level == 5) & (subjectData.id == id)]\n","subjectData = subjectData[['roi', 'volume']]\n","## Merge the subject data with the multilevel data\n","subjectData = pd.merge(subjectData, multilevel_lookup, on = \"roi\")\n","subjectData = subjectData.assign(icv = \"ICV\")\n","subjectData = subjectData.assign(comp = subjectData.volume / np.sum(subjectData.volume))\n","subjectData.head()"]},{"cell_type":"code","execution_count":null,"id":"39b53202-1d88-41ff-a459-8187c89b5b75","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":817},"id":"39b53202-1d88-41ff-a459-8187c89b5b75","executionInfo":{"status":"ok","timestamp":1713275020216,"user_tz":240,"elapsed":531,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"f623452b-e9a4-4abf-da78-9f701ec6bfc9"},"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}],"source":["fig = px.sunburst(subjectData, path=['icv', 'level1', 'level2', 'level3', 'level4', 'roi'],\n"," values='comp', width=800, height=800)\n","fig.show()"]},{"cell_type":"markdown","id":"baa4f35b-d12c-4757-ba00-8ad591c2cd9a","metadata":{"id":"baa4f35b-d12c-4757-ba00-8ad591c2cd9a"},"source":["Similarly, we can make a treemap."]},{"cell_type":"code","execution_count":null,"id":"52bce72a-5b86-4704-a107-559f6e86dee8","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":817},"id":"52bce72a-5b86-4704-a107-559f6e86dee8","executionInfo":{"status":"ok","timestamp":1713275020947,"user_tz":240,"elapsed":742,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"0d9f74e2-9302-4882-a0ab-f0ee1830cfc7"},"outputs":[{"output_type":"display_data","data":{"text/html":["\n","\n","\n","
\n","
\n","\n",""]},"metadata":{}}],"source":["fig = px.treemap(subjectData,\n"," path = ['icv', 'level1', 'level2', 'level3', 'level4', 'roi'],\n"," values='comp',\n"," color='comp',\n"," color_continuous_scale = 'RdBu',\n"," color_continuous_midpoint = .005,\n"," width=800, height=800\n"," )\n","fig.show()"]},{"cell_type":"markdown","id":"4d1e2156-449c-43d9-aef9-8205a7afbf05","metadata":{"id":"4d1e2156-449c-43d9-aef9-8205a7afbf05"},"source":["## Interactive maps using folium and leaflet\n","\n","A common form of interactive graphic is a map. There are several mapping libraries for python, including some in plotly. folium is another option that connects to the well known leaflet javascript library. Let's create a quick plot of the Bloomberg School of Public Health Building, which is at latitude and longitude 39.298, -76.590. If you haven't already, pip or conda install folium."]},{"cell_type":"code","execution_count":null,"id":"1f2080bc-3342-48c8-aa88-2ebbe6dd9871","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"1f2080bc-3342-48c8-aa88-2ebbe6dd9871","executionInfo":{"status":"ok","timestamp":1713275021612,"user_tz":240,"elapsed":698,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"a0bb903f-7b64-4c56-f0bc-33e78e74b0ed"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[""],"text/html":["
Make this Notebook Trusted to load map: File -> Trust Notebook
"]},"metadata":{},"execution_count":24}],"source":["import folium\n","\n","m = folium.Map(location = [39.298, -76.590], zoom_start = 15)\n","m"]},{"cell_type":"markdown","id":"da98c3e9-fb3a-4aaf-b45d-64fc84877917","metadata":{"id":"da98c3e9-fb3a-4aaf-b45d-64fc84877917"},"source":["You can then add elements to the map. For example, suppose we want a marker on the building saying \"my office\". It's just that easy! This is truly just the tip of the iceberg of using folium/leaflet."]},{"cell_type":"code","execution_count":null,"id":"f9e84c3e-829f-44db-a637-cf0580999040","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":1000},"id":"f9e84c3e-829f-44db-a637-cf0580999040","executionInfo":{"status":"ok","timestamp":1713275021614,"user_tz":240,"elapsed":69,"user":{"displayName":"Brian Caffo","userId":"07979705296072332292"}},"outputId":"d56b3435-6a5b-4544-e7e7-afefb63ba22e"},"outputs":[{"output_type":"execute_result","data":{"text/plain":[""],"text/html":["
Make this Notebook Trusted to load map: File -> Trust Notebook
"]},"metadata":{},"execution_count":25}],"source":["folium.Marker([39.298, -76.590], popup = \"What it says when you click\", tooltip = \"What it says when you hover\").add_to(m)\n","m"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.10.13"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":5}