{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Attaching package: ‘igraph’\n",
      "\n",
      "\n",
      "The following objects are masked from ‘package:stats’:\n",
      "\n",
      "    decompose, spectrum\n",
      "\n",
      "\n",
      "The following object is masked from ‘package:base’:\n",
      "\n",
      "    union\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 75920 characters]",
      "text/plain": [
       "Plot with title “”"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 420,
       "width": 420
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# library\n",
    "# install.packages(\"IRKernel\", \"igraph\", \"RColorBrewer\")\n",
    "library(igraph)\n",
    "library(RColorBrewer)\n",
    "\n",
    "# create data:\n",
    "links <- data.frame(\n",
    "    source=c(\"A\",\"A\", \"A\", \"A\", \"A\",\"J\", \"B\", \"B\", \"C\", \"C\", \"D\",\"I\"),\n",
    "    target=c(\"B\",\"B\", \"C\", \"D\", \"J\",\"A\",\"E\", \"F\", \"G\", \"H\", \"I\",\"I\"),\n",
    "    importance=(sample(1:4, 12, replace=T))\n",
    "    )\n",
    "nodes <- data.frame(\n",
    "    name=LETTERS[1:10],\n",
    "    carac=c( rep(\"young\",3),rep(\"adult\",2), rep(\"old\",5))\n",
    ")\n",
    " \n",
    "# Turn it into igraph object\n",
    "network <- graph_from_data_frame(d=links, vertices=nodes, directed=F) \n",
    " \n",
    "# Make a palette of 3 colors\n",
    "coul  <- brewer.pal(3, \"Set1\") \n",
    "\n",
    "# Create a vector of color\n",
    "my_color <- coul[as.numeric(as.factor(V(network)$carac))]\n",
    " \n",
    "# Make the plot\n",
    "plot(network, vertex.color=my_color)\n",
    " \n",
    "# Add a legend\n",
    "legend(\"bottomleft\", legend=levels(as.factor(V(network)$carac))  , col = coul , bty = \"n\", pch=20 , pt.cex = 3, cex = 1.5, text.col=coul , horiz = FALSE, inset = c(0.1, 0.1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.1.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
