Skip to content

Commit 1dc4509

Browse files
committed
Add R code for locations example
1 parent 3fc5155 commit 1dc4509

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

session3_scientific_ecosystem/slides_session3.ipynb

+38
Original file line numberDiff line numberDiff line change
@@ -3408,6 +3408,44 @@
34083408
"ax[0].figure.set_figwidth(12)"
34093409
]
34103410
},
3411+
{
3412+
"cell_type": "markdown",
3413+
"metadata": {
3414+
"slideshow": {
3415+
"slide_type": "slide"
3416+
}
3417+
},
3418+
"source": [
3419+
"The same thing in R:\n",
3420+
"\n",
3421+
"```\n",
3422+
"set.seed(42)\n",
3423+
"\n",
3424+
"canvas_m = 0.3\n",
3425+
"canvas_r = 900\n",
3426+
"\n",
3427+
"locations_n = 25\n",
3428+
"neighbours_n = 4\n",
3429+
"\n",
3430+
"locations = matrix(runif(2*locations_n), ncol=2, \n",
3431+
" dimnames=list(NULL, c('x', 'y')))\n",
3432+
"\n",
3433+
"canvas_l = seq(-canvas_m, canvas_m + 1, length=canvas_r)\n",
3434+
"canvas_c = as.matrix(expand.grid(x = canvas_l, y = canvas_l))\n",
3435+
"\n",
3436+
"canvas_c = cbind(canvas_c, v = apply(canvas_c, 1, function(p) {\n",
3437+
" d = sqrt((locations[, 1] - p[1])**2 + (locations[, 2] - p[2])**2)\n",
3438+
" sum(locations_n**order(d)[1:neighbours_n])\n",
3439+
"}))\n",
3440+
"\n",
3441+
"library(ggplot2)\n",
3442+
"ggplot(as.data.frame(canvas_c), aes(x = x, y = y)) +\n",
3443+
" geom_tile(aes(fill = factor(v))) + \n",
3444+
" geom_point(data = as.data.frame(locations), color = 'red') +\n",
3445+
" scale_fill_discrete(guide=FALSE)\n",
3446+
"```"
3447+
]
3448+
},
34113449
{
34123450
"cell_type": "markdown",
34133451
"metadata": {

0 commit comments

Comments
 (0)