I'm brand new to R and currently am stuck while working on a social network. I'm using the package igraph to create the network.
The way I'm calling for the plot is:
plot(k, layout=layout.fruchterman.reingold, vertex.label=V(k)$Rank)
Is it possible to have multiple labels on a vertex?
You can use paste to concatenate the labels together. Here I choose dummy labels ,
labe is lower letter:UPPER LETTER
library(igraph)
g <- graph.ring(5)
V(g)$size <- 100
V(g)$label=paste(letters[1:5],LETTERS[1:5],sep=':')
V(g)$label.cex <- 2
plot(g)
Related
I would like to compare two plots of graphs(an observed graph and a simulated one) that have the exact same nodes.
I would like to keep the nodes position fix so I can compare the difference in the edge's distribution.
I have tried set.seed but it's just keeping the plot identical every time I run it.
Is there a way to take the layout of a graph and use it for the other?
Thanks,
Fwiw, I guess you can use the layout argument of plot:
library(igraph)
set.seed(1)
g1 <- ba.game(20, dir=F)
g2 <- ba.game(20, dir=F)
par(mfrow = c(1, 2))
coords <- layout.fruchterman.reingold(g1)
plot(g1, layout = coords)
plot(g2, layout = coords)
I'm making a network plot in R using iGraph. I first plot it using tkplot() so that I can manually reposition some of the nodes. Then I capture the new coordinates and then insert those in the plot function to replot the graph along with additional adjustments (changing the opacity of the nodes).
The problem is that even when using the tkplot.coords coordinates, the second graph doesn't look like the tkplot. Instead, some of the arrow heads appear in the middle of the edge rather than at the end, and the nodes are tightly clustered and overlapping, even though that isn't the case with the tkplot. Any suggestions for how I can get the plot() function to exactly mimic the plot produced using tkplot()?
I am using R Studio, so I am wondering if there is a conversion issue with that.
My simplified code is as follows:
Net1 <- graph.data.frame(myedgedata, vertices=nodeslist, directed=TRUE)
g <- graph.adjacency(get.adjacency(Net1), weighted = TRUE)
E(g)$weight <- E(g)$weight+1
tkplot(g)
coords <- tkplot.getcoords(1)
plot(g, edge.width=E(g)$weight, vertex.color = adjustcolor(nodeslist$colors, alpha=.5), layout=coords)
I need help changing the vertex labels on graphs produced using bnlearn.
First, I run the program to get an undirected graph. When I plot the graph with plot(data) the vertices are labelled with "V1,V2,V3...". Instead of this I want to plot them with their real labels.
The default plot should have the variable names as the node labels - you will need to share some code to show why this is not so. However, below is a method to manually change the node labels.
library(bnlearn)
library(Rgraphviz)
m <- hc(learning.test)
par(mfrow=c(1,2))
#default plot
g <- graphviz.plot(m)
# change labels
z <- paste0("newlab_", letters[1:numNodes(g)])
names(z) <- nodes(g)
nAttrs <- list()
nAttrs$label <- z
# updated plots
plot(g, nodeAttrs=nAttrs)
For more info on Rgraphviz see How To Plot A Graph Using Rgraphviz, Jeff Gentry
I am trying to create a dendrogram of the communities only of a network. The example code below gives me a dendrogram of all the nodes, but as I work with a relatively large dataset, I would like to create a dendrogram of only the comunities,so that I would have a smaller dendrogram with only the communities, is this possible?
library(igraph)
set.seed(1)
g001 <- erdos.renyi.game(100, 1/10, directed = FALSE)
fc01 <- fastgreedy.community(g001)
colors <- rainbow(max(membership(fc01)))
plot(g001, vertex.size=2, vertex.label=NA, vertex.color=colors[membership(fc01)] )
dendPlot(fc01, mode="phylo", cex=1)
Thank you.
The dendrogram class has a cut function you can use to split up a dendrogram at a certain height. The community algorithm seems to use heights based on how many objects there are. Therefore, given your fc01 object above, you can split it into subgroups with
ss01 <- cut(as.dendrogram(fc01), h=length(membership(fc01))-length(fc01))$lower
That created 5 groups for me. We can plot the entire set and the 5 subsets with
layout(matrix(1:6, nrow=2))
dendPlot(fc01, mode="hclust")
lapply(ss01, plot, cex=1)
So each sub-graph is in ss01[[1]], ss02[[2]], etc...
How can I create a cluster plot in R without using clustplot?
I am trying to get to grips with some clustering (using R) and visualisation (using HTML5 Canvas).
Basically, I want to create a cluster plot but instead of plotting the data, I want to get a set of 2D points or coordinates that I can pull into canvas and do something might pretty with (but I am unsure of how to do this). I would imagine that I:
Create a similarity matrix for the entire dataset (using dist)
Cluster the similarity matrix using kmeans or something similar (using kmeans)
Plot the result using MDS or PCA - but I am unsure of how steps 2 and 3 relate (cmdscale).
I've checked out questions here, here and here (with the last one being of most use).
Did you mean something like this?
Sorry but i know nothing about HTML5 Canvas, only R... But I hope it helps...
First I cluster the data using kmeans (note that I did not cluster the distance matrix), than I compute the distance matix and plot it using cmdscale. Then I add colors to the MDS-plot that correspond to the groups identified by kmeans. Plus some nice additional graphical features.
You can access the coordinates from the object created by cmdscale.
### some sample data
require(vegan)
data(dune)
# kmeans
kclus <- kmeans(dune,centers= 4, iter.max=1000, nstart=10000)
# distance matrix
dune_dist <- dist(dune)
# Multidimensional scaling
cmd <- cmdscale(dune_dist)
# plot MDS, with colors by groups from kmeans
groups <- levels(factor(kclus$cluster))
ordiplot(cmd, type = "n")
cols <- c("steelblue", "darkred", "darkgreen", "pink")
for(i in seq_along(groups)){
points(cmd[factor(kclus$cluster) == groups[i], ], col = cols[i], pch = 16)
}
# add spider and hull
ordispider(cmd, factor(kclus$cluster), label = TRUE)
ordihull(cmd, factor(kclus$cluster), lty = "dotted")
Here you can find one graph to analyze cluster results, "coordinate plot", within "clusplot" package.
It is not based on PCA. It uses function scale to have all the variables means in a range of 0 to 1, so you can compare which cluster holds the max/min average for each variable.
install.packages("devtools") ## To be able to download packages from github
library(devtools)
install_github("pablo14/clusplus")
library(clusplus)
## Create k-means model with 3 clusters
fit_mtcars=kmeans(mtcars,3)
## Call the function
plot_clus_coord(fit_mtcars, mtcars)
This post explains how to use it.