Using vegan to create minimum spanning tree:
library(vegan)
MST <- spantree(D)
where D is a distance / dissimilarity matrix amongst N entities.
I then plot using:
plot(MST, type = "t")
I now want to color the different nodes of the tree, depending on a label I have in DataSet$SubGroups, indicating which subgroup each of the N entities belong to. So that for instance, for all entities for which DataSet$SubGroups == SubGroup1, I want their label to be Green, for Subgroup2 Blue etc.
I tried with nodelabels from the ape package, but no luck as I get:
Error in get("last_plot.phylo", envir = .PlotPhyloEnv) : object
'last_plot.phylo' not found
Anybody any idea on how to get the color right?
Is it also possible to specify RGB color codes?
Related
I want to generate a dendrogram out of an association matrix (i.e., a squared matrix that contains association data for each pair of individuals in my population). The dendrogram should show the association index (scale 0 to 1) on the y axis and individuals as leaf labels.
I did as follows:
#Loaded my data i.e., association matrix :
HWI<-as.matrix(read.csv2(HWI.csv))
#Computed distances:
data.dist=dist(HWI, method = "euclidean")
#Did the clustering using the 'average-linkage method':
data.hclust=hclust(data.dist,method="average")
#Plotted the dendrogram:
hcd <- as.dendrogram(data.hclust)
plot(hcd, type = "rectangle", xlab = "Associative distance between individuals",horiz = TRUE)
The problem is, the height of the dendrogram is the distance measure, on which the clustering is based, and not the association index.
Does anyone know how I can plot the dendrogram with the association index (original data file) as y-axis instead?
Thanks a million for your suggestions.
Eve
I have a very large bipartite network model that I created from 5 million lines of a dataset. I decompose my network model because I can not draw a graph of this size. Now all I need is to plot the decompose graphics one by one. There is no problem with that. But I want to draw the graph with a shape according to the attributes of each node. For example, I want a square for the "A" attributes on my graph G, and a triangle for the "B" attributes. In addition to this I want to add vertex labels by attributes. Here is my codes to plot first component of graph after creating bipartite G and its work:
components <- decompose(G)
plot(components[[1]])
I tried something like this to adding labels and changing vertex shapes according to graph attributes but it didn't work:
plot(components[[1]], vertex.label= V(G)$attributes,
vertex.shape=c("square", "triangle"))
Does anyone can help me, I'm stuck. Thank you so much!
the components function returns a list of vertices which make up a component. So you need to traverse the list, create a subgraph and plot. As for plotting attributes you need to provide a reproducible example for us to help.
library(igraph)
set.seed(8675309)
g <- sample_gnp(200, p = 0.01)
V(g)$name <- paste0("Node", 1:vcount(g))
V(g)$shape <- sample(c("circle","square"), vcount(g), replace = T)
clu <- components(g)
grps <- groups(clu)
lapply(grps, function(x) plot(induced_subgraph(g, x)))
I'm trying to set a fill color in Rgraphviz for a subgraph. In the documentation, it is implied that you can set subgraph attributes where you set whether or not the subgraph is a cluster, like so:
subGList = list()
subGList[[1]] = list(graph=sg.iip, attrs=c(fillcolor="lightgreen", style = "filled",
shape = "rectangle"), cluster = TRUE)
Unfortunately, setting attributes in this manner doesn't seem to have any effect on the plot.
Because my subgraphs contain 20-30 nodes (where the full graph has 250 nodes), it's not really reasonable to code it with the nodewise method where you have to use each node name and set it to the color.
edit My current workaround is to do:
vc <- as.list(rep("lightgreen", length(subgraphNames)))
names(vc) <- subgraphNames
plot(fG.iip.NEL, 'neato', subGList=subGList, nodeAttrs = list(fillcolor=vc))
In Rgraphviz, how can I assign nodes and edges to particular layers, and plot only a selected layer or layers?
After perusing and searching the Rgraphviz documentation, I think I've figured out how to assign nodes and edges to particular layers -- but I still can't figure out how to plot only a selected layer.
This "How to use layers" item on the graphviz wiki implies that there should be a "layerselect" graph attribute that can be used to plot only a specified layer. However, the list of allowed graph attributes in Rgraphviz does not include "layerselect." The "layers" graph attribute says "Only those components belonging to the current layer appear," but I can find no information on how to set (or even query) the "current layer."
I tried to do it anyway, but wasn't successful. Here's a reproducible example of my attempt:
require('Rgraphviz')
params <- LETTERS[1:5] #nodes A, B, C, D, E
edgelist <- vector('list', #set up edges
length=length(params))
names(edgelist) <- params
edgelist[['B']] <- c('E','A') #add edges from B to E and from B to A
edgelist[['A']] <- 'C' #add edge from A to C
edgelist[['D']] <- 'E' #add edge from D to E
graph.nel <- new('graphNEL', #construct graphNEL object
nodes=params,
edgeL=edgelist,
edgemode='directed')
#I want there to be two layers:
#"redlayer", containing nodes A, B, C, E and the edges between them;
#"blacklayer", containing node D and the edge from D to E.
#Assign the colors and layers of the edges
eAttr <- list(color=c('B~A'='red',
'A~C'='red',
'B~E'='red',
'D~E'='black'),
layer=c('B~A'='redlayer',
'A~C'='redlayer',
'B~E'='redlayer',
'D~E'='blacklayer')
)
#Assign the colors and layers of the nodes
nAttr <- list(color=c(B='red',
A='red',
C='red',
E='red',
D='black'),
layer=c(B='redlayer',
A='redlayer',
C='redlayer',
E='redlayer',
D='blacklayer'))
#Now plot
plot(graph.nel,
attrs=list(graph=list(layers='redlayer:blacklayer', #Define the two layers
layersep=':', #Explicitly define the layer separation character
layerselect='redlayer')), #Attempt to select only the 'redlayer' for plotting
edgeAttrs=eAttr, #specify edge attributes
nodeAttrs=nAttr #specify node attributes
)
The result of the above code is the following plot:
However, I expected that only the nodes and edges colored red (i.e. those assigned to the layer named 'redlayer') would appear!
I've also tried
plot(graph.nel,
attrs=list(graph=list(layers='redlayer'), #Attempt to select only the 'redlayer' for plotting
edgeAttrs=eAttr, #specify edge attributes
nodeAttrs=nAttr #specify node attributes
)
but it results in exactly the same plot -- that is, both layers are still being plotted.
Is there any way to plot only the layer named 'redlayer' in this example?
I am trying to change the color of the vertexes in an igraph generated graph.
To be more specific, I have a 95 nodes graph created from an adjacency matrix and I would like to color them according to their degree/betweenness/eigenvalue centrality/closeness but I'm guessing that after I know how to do it with one, I'll be able to do it with others.
So I've coded the basics of graph generation until now:
dataset <- read.csv("~/Google Drive/Cours M2/Network Economics/Data/Collabs_2013.csv", sep=";")
matrix<-as.matrix(dataset)
adj<-graph.adjacency(matrix)
plot(adj)
btw<-betweenness(adj,directed = FALSE)
I now have a vector of 95 values of betweennesses and I would like to plot a graph with a gradient of colors that follows the betweenness values (e.g. from red for the lowest value to green to the highest). I'm guessing I have to mess with vertex's attributes but I have no idea how to input the vector as a color attribute.
Seems like you already did most of the work. All you have to know is colorRamppalette and setting the vertex.color for the network. Assuming you have to change the colors linearly,
just do
fine = 500 # this will adjust the resolving power.
pal = colorRampPalette(c('red','green'))
#this gives you the colors you want for every point
graphCol = pal(fine)[as.numeric(cut(btw,breaks = fine))]
# now you just need to plot it with those colors
plot(adj, vertex.color=graphCol)
credits to this. I was using a much more inefficient method to assign the colors before answering this.
Just a note:
It can be problematic to define
palette = colorRampPalette(c('blue','green'))
as the 'palette' function, is also used by igraph, and so igraph later produces as error.
See problem Color pallette for vertices in igraph network in R