Skip to content

Commit 84f2022

Browse files
committed
Draw the heatmap row with geom_tile on precomputed counts
ggplot2's geom_bin2d bins discrete positions with a continuous binwidth, so the R heatmap came out as tiny disconnected tiles. Counting first and drawing geom_tile is the idiomatic recipe and matches the pivot-then-draw approach the matplotlib, seaborn, and hvPlot columns already take. Also stop seaborn from rotating the clarity labels.
1 parent 48bbc09 commit 84f2022

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

Examples.ipynb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,9 +3044,13 @@
30443044
"outputs": [],
30453045
"source": [
30463046
"%%R -w 10 -h 10 -u in\n",
3047-
"ggplot(data=diamonds) +\n",
3048-
" aes(x=cut, y=clarity) +\n",
3049-
" geom_bin2d()"
3047+
"counts <- as.data.frame(\n",
3048+
" table(cut=diamonds$cut,\n",
3049+
" clarity=diamonds$clarity),\n",
3050+
" responseName=\"count\")\n",
3051+
"ggplot(data=counts) +\n",
3052+
" aes(x=cut, y=clarity, fill=count) +\n",
3053+
" geom_tile()"
30503054
]
30513055
},
30523056
{
@@ -3061,9 +3065,13 @@
30613065
},
30623066
"outputs": [],
30633067
"source": [
3064-
"(ggplot(diamonds) +\n",
3065-
" aes(x=\"cut\", y=\"clarity\") +\n",
3066-
" geom_bin2d())"
3068+
"counts = (diamonds\n",
3069+
" .groupby([\"cut\", \"clarity\"])\n",
3070+
" .size()\n",
3071+
" .reset_index(name=\"count\"))\n",
3072+
"(ggplot(counts) +\n",
3073+
" aes(x=\"cut\", y=\"clarity\", fill=\"count\") +\n",
3074+
" geom_tile())"
30673075
]
30683076
},
30693077
{
@@ -3078,9 +3086,14 @@
30783086
},
30793087
"outputs": [],
30803088
"source": [
3081-
"(lp.ggplot(diamonds) +\n",
3082-
" lp.aes(x=\"cut\", y=\"clarity\") +\n",
3083-
" lp.geom_bin2d())"
3089+
"counts = (diamonds\n",
3090+
" .groupby([\"cut\", \"clarity\"])\n",
3091+
" .size()\n",
3092+
" .reset_index(name=\"count\"))\n",
3093+
"(lp.ggplot(counts) +\n",
3094+
" lp.aes(x=\"cut\", y=\"clarity\",\n",
3095+
" fill=\"count\") +\n",
3096+
" lp.geom_tile())"
30843097
]
30853098
},
30863099
{
@@ -3198,8 +3211,9 @@
31983211
" .groupby(['clarity', 'cut'])\n",
31993212
" .size()\n",
32003213
" .unstack())\n",
3201-
"sns.heatmap(counts,\n",
3202-
" cbar_kws=dict(label='count'));"
3214+
"ax = sns.heatmap(\n",
3215+
" counts, cbar_kws=dict(label='count'))\n",
3216+
"ax.tick_params(axis='y', rotation=0);"
32033217
]
32043218
}
32053219
],

0 commit comments

Comments
 (0)