{"id":3137,"date":"2016-10-19T10:21:19","date_gmt":"2016-10-19T09:21:19","guid":{"rendered":"http:\/\/www.blopig.com\/blog\/?p=3137"},"modified":"2016-10-19T10:21:19","modified_gmt":"2016-10-19T09:21:19","slug":"plotting-and-storing-a-3d-network-in-r","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2016\/10\/plotting-and-storing-a-3d-network-in-r\/","title":{"rendered":"Plotting and storing a 3D network in R"},"content":{"rendered":"<p>A simple toy example of a three layered network:<\/p>\n<p>Note 1:\u00a0In order to view the 3D plots, mac users will need\u00a0Xquartz \u00a0installed (<a href=\"https:\/\/www.xquartz.org\/\">https:\/\/www.xquartz.org\/<\/a>).<\/p>\n<pre> \r\nrequire(igraph)\r\nrequire(rgl)\r\n#Another package that might be needed is \"rglwidget\". The function writeWebGL will show an error stating if rglwidget is required.\r\n######################################\/\/\/\/ \r\n######The basics######################\/\/\/\/\r\n######################################\/\/\/\/\r\n#1) Create a \"food\" network (three layers) \r\nset.seed(432)\r\ng1&lt;-watts.strogatz.game(dim = 1,size = 5,nei = 2,p = .5,loops = FALSE,multiple = FALSE)\r\ng2&lt;-watts.strogatz.game(dim = 1,size = 10,nei = 2,p = .2,loops = FALSE,multiple = FALSE)\r\ng3&lt;-watts.strogatz.game(dim = 1,size = 30,nei = 1,p = .5,loops = FALSE,multiple = FALSE)\r\ng123=g1+g2+g3 \r\n\r\n\r\n#Create more edges btw layers \r\ng123=rewire(g123,each_edge(prob=.4,loops = FALSE,multiple = FALSE)) \r\nne=15;add_edges(g123,edges = cbind(sample(1:vcount(g1),size = ne,replace = TRUE), sample((vcount(g1)+1):vcount(g123),size = ne,replace = TRUE)))#top layer \r\nne=30;add_edges(g123,edges = cbind(sample((vcount(g1)+1):(vcount(g1)+vcount(g2)),size = ne,replace = TRUE), sample((vcount(g1)+vcount(g2)+1):vcount(g123),size = ne,replace = TRUE)))#second layer \r\n\r\n#A quick plot of the graph\r\nplot(g123,vertex.size=1,vertex.label.cex=0.02)\r\n\r\n\r\n#Create 3d coordinates of the network layout\r\ncircpos=function(n,r=1){#Coordinates on a circle\r\n rad=seq(0,2*pi,length.out=n+1)[-1];x=cos(rad)*r;y=sin(rad)*r\r\n return(cbind(x,y))\r\n}\r\n#\r\nlay=rbind(cbind(circpos(vcount(g1),r=1), runif(n = vcount(g1),-1,1)),\r\n cbind(circpos(vcount(g2),r=2), runif(n = vcount(g2),6,7)),\r\n cbind(circpos(vcount(g3),r=4), runif(n = vcount(g3),13,17))\r\n)\r\n\r\n\r\n\r\n#2d plot using the previous layout\r\nplot(g123,vertex.size=5,vertex.label=NA,layout=lay[,c(1,3)])\r\nplot(g123,vertex.size=1,vertex.label=NA,layout=lay[,c(1,2)])\r\n\r\n<\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/layers.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-3141 aligncenter\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/layers.png?resize=470%2C456&#038;ssl=1\" alt=\"layers\" width=\"470\" height=\"456\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/layers.png?w=470&amp;ssl=1 470w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/layers.png?resize=300%2C291&amp;ssl=1 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a><\/p>\n<pre> \r\n#3D graph plot\r\n#Add some colour to nodes and edges\r\nnodecols=c(rep(\"red\",vcount(g1)),\r\n rep(\"blue\",vcount(g2)),\r\n rep(\"yellow\",vcount(g3)))\r\n\r\nedgecols=function(elist,cols,grouplist){\r\n whatcol=rep(length(cols)+1,nrow(elist))\r\n finalcol=whatcol\r\n for(i in 1:nrow(elist)){\r\n for(k in length(cols):1){ \r\n if( k * (length( intersect(elist[i,], grouplist[[k]]) ) &gt; 0)){\r\n whatcol[i]=min(whatcol[i], k )\r\n }\r\n }\r\n finalcol[i]=cols[whatcol[i]]\r\n }\r\n return(finalcol)\r\n}\r\n\r\n#Open 3d viewer\r\nrgl.open()\r\nrglplot(g123, layout=lay,vertex.size=5,vertex.label=NA,vertex.color=nodecols,\r\n edge.color=edgecols(elist=get.edgelist(g123,names = FALSE),cols=c(\"orange\",\"green\",\"pink\"),grouplist=list(1:vcount(g1), (vcount(g1)+1):(vcount(g1)+vcount(g2)), (vcount(g1)+vcount(g2)+1):vcount(g123)) )\r\n)<\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/3d_layers.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-3139 aligncenter\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/3d_layers.png?resize=470%2C319&#038;ssl=1\" alt=\"3d_layers\" width=\"470\" height=\"319\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/3d_layers.png?w=470&amp;ssl=1 470w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/3d_layers.png?resize=300%2C204&amp;ssl=1 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a><\/p>\n<pre>###Storing the plot in an html file###\r\n\r\ndirfolder=\"...\" #your dir\r\n#rgl.open()#instead of rgl.open use open3d, in order to save the plot. \r\nopen3d()\r\nrglplot(g123, layout=lay,vertex.size=5,vertex.label=NA,vertex.color=nodecols,\r\n edge.color=edgecols(elist=get.edgelist(g123,names = FALSE),cols=c(\"orange\",\"green\",\"pink\"),grouplist=list(1:vcount(g1), (vcount(g1)+1):(vcount(g1)+vcount(g2)), (vcount(g1)+vcount(g2)+1):vcount(g123)) )\r\n)\r\n#Fix the view\r\nrgl.viewpoint(theta=90, phi=0)\r\n\r\n#Save a static 2d image:\r\nrgl.snapshot(paste(dirfolder,\"a_png_pic_0.png\",sep=\"\"), fmt=\"png\", top=TRUE)\r\n\r\n#Save the plot in a .htlm file:\r\nrglfolder=writeWebGL(dir = paste(dirfolder,\"first_net3d\",sep=\"\"), width=700)\r\n\r\n#The previous function should create a file called index.htlm inside the folder \"first_net3d\". By opening this file in a browser (with javascript enabled) the 3d plot will be displayed again.\r\n#Also the following command will open the plot in the browser:\r\nbrowseURL(rglfolder)\r\n<\/pre>\n<p>Note 2: In order to view\u00a0the .htlm file javascript should be enabled in the browser. (Here is an example on how to do this for <a href=\"http:\/\/support.ezimerchant.com\/hc\/en-us\/articles\/200836150-How-do-I-enable-cookies-and-JavaScript-in-Safari-Mac-\" target=\"_blank\">safari<\/a> ).<\/p>\n<p>Although not covered in the previous script, further options are available such as edge\/vertex size and the ability to control independently each of the nodes and edges in the graph. Here is an example that makes more use of\u00a0these options:<\/p>\n<div id=\"attachment_3140\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/clour_plot.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" aria-describedby=\"caption-attachment-3140\" loading=\"lazy\" class=\"wp-image-3140 size-full\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/clour_plot.png?resize=470%2C581&#038;ssl=1\" alt=\"clour_plot\" width=\"470\" height=\"581\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/clour_plot.png?w=470&amp;ssl=1 470w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/clour_plot.png?resize=243%2C300&amp;ssl=1 243w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a><p id=\"caption-attachment-3140\" class=\"wp-caption-text\">3d network representing a\u00a0T cell receptor. Edges are coloured according to a relevant path found between the bottom green node and the upper red node cluster.<\/p><\/div>\n<div id=\"attachment_3151\" style=\"width: 173px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/tcell_receptor.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" aria-describedby=\"caption-attachment-3151\" loading=\"lazy\" class=\"wp-image-3151 size-medium\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/tcell_receptor.png?resize=163%2C300&#038;ssl=1\" alt=\"T cell receptor (in blue), binding to a peptide (in red).\" width=\"163\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/tcell_receptor.png?resize=163%2C300&amp;ssl=1 163w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2016\/10\/tcell_receptor.png?w=494&amp;ssl=1 494w\" sizes=\"auto, (max-width: 163px) 100vw, 163px\" \/><\/a><p id=\"caption-attachment-3151\" class=\"wp-caption-text\">T cell receptor (in blue), binding to a peptide (in red).<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A simple toy example of a three layered network: Note 1:\u00a0In order to view the 3D plots, mac users will need\u00a0Xquartz \u00a0installed (https:\/\/www.xquartz.org\/). require(igraph) require(rgl) #Another package that might be needed is &#8220;rglwidget&#8221;. The function writeWebGL will show an error stating if rglwidget is required. ######################################\/\/\/\/ ######The basics######################\/\/\/\/ ######################################\/\/\/\/ #1) Create a &#8220;food&#8221; network (three [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,14],"tags":[],"ppma_author":[514],"class_list":["post-3137","post","type-post","status-publish","format-standard","hentry","category-code","category-howto"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":514,"user_id":26,"is_guest":0,"slug":"luis","display_name":"Luis Ospina Forero","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/310cef32cd5dac5a383fe35d2e6fa0ed40cb03d0712d2b5a5ef81092db812b3e?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/3137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=3137"}],"version-history":[{"count":4,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/3137\/revisions"}],"predecessor-version":[{"id":3153,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/3137\/revisions\/3153"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=3137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=3137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=3137"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=3137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}