A correlation network is a network chart made from a correlation matrix. Here we take the example of the **mtcars dataset. **For 32 cars we have several features. We can compute the correlation between each pair of car using the **cor** function:

1 2 3 4 5 6 7 8 |
# library library(igraph) # data head(mtcars) # Make a correlation matrix: mat=cor(t(mtcars[,c(1,3:6)])) |

This is an adjacency matrix (square) that we can give to Igraph using the graph_from_adjacency_matrix() function. (see graph xxx). Note that we keep only connections stronger than 0.995.

1 2 3 4 5 |
# Keep only high correlations mat[mat<0.995]=0 # Make an Igraph object from this matrix: network=graph_from_adjacency_matrix( mat, weighted=T, mode="undirected", diag=F) |

Now it is easy to compute the default network chart

1 |
plot(network) |

But it is more interesting to map a color on it and add a few customizations:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# color palette library(RColorBrewer) coul = brewer.pal(nlevels(as.factor(mtcars$cyl)), "Set2") # Map the color to cylinders my_color=coul[as.numeric(as.factor(mtcars$cyl))] # plot par(bg="grey13", mar=c(0,0,0,0)) set.seed(4) plot(network, vertex.size=12, vertex.color=my_color, vertex.label.cex=0.7, vertex.label.color="white", vertex.frame.color="transparent" ) # title and legend text(0,0,"The network chart of the mtcars dataset",col="white") text(0.2,-0.1," - by the R graph gallery",col="white") legend(x=-0.6, y=-0.12, legend=paste( levels(as.factor(mtcars$cyl)), " cylinders", sep=""), col = coul , bty = "n", pch=20 , pt.cex = 2, cex = 1, text.col="white" , horiz = T) |

Not what you are looking for ? Make a new search !

`[mediatagger]`

## Leave a Reply