I have a fblog data set ,PolParty is one attribute of my data, I want to plot just 2 political parties (say P1 and P2) and plot the network of blogs.
i wrote code below but i think it is wrong , can some one help me?
library(statnet)
library(igraph)
library(sand)
data(fblog)
fblog = upgrade_graph(fblog)
class(fblog)
summary(fblog)
V(fblog)$PolParty
table(V(fblog)$PolParty)
p1<-V(fblog)[PolParty=="PS"] # by their labels/names
p2<-V(fblog)[PolParty=="UDF"]
class(p1)
The objects p1 and p2 that you were creating are of the class igraph.vs (instead of igraph). This object just documents the vertices. Which is not a full graph. Hence when you try to plot it you don't get anything.
Based on the following post: Subset igraph graph by label
g=subgraph.edges(graph=fblog, eids=which(V(fblog)$PolParty==" PS"), delete.vertices = TRUE)
plot(g)
The above works. NOTE: regarding the output of V(fblog)$PolParty- everything is preceeded by a space hence you need to use V(fblog)$PolParty==" PS"
UPDATE: if I want to subset based on 2 conditions- I will modify the which() command:
g=subgraph.edges(graph=fblog, eids=which(V(fblog)$PolParty==" PS"| V(fblog)$PolParty==" UDF"), delete.vertices = TRUE)
plot(g)
Related
I am using the statspat package because I am working on spatial patterns.
I would like to do in ggplot and with colors instead of numbers (because it is not too readable),
the following graph, produced with the plot.quadratest function: Polygone
The numbers that interest me for the intensity of the colors are those at the bottom of each box.
The test object contains the following data:
Test object
I have looked at the help of the function, as well as the code of the function but I still cannot manage it.
Ideally I would like my final figure to look like this (maybe not with the same colors haha):
Final object
Thanks in advance for your help.
Please provide a reproducible example in the future.
The package reprex may be very helpful.
To use ggplot2 for this my best bet would be to convert
spatstat objects to sf and do the plotting that way,
but it may take some time. If you are willing to use base
graphics and spatstat you could do something like:
library(spatstat)
# Data (using a built-in dataset):
X <- unmark(chorley)
plot(X, main = "")
# Test:
test <- quadrat.test(X, nx = 4)
# Default plot:
plot(test, main = "")
# Extract the the `quadratcount` object (regions with observed counts):
counts <- attr(test, "quadratcount")
# Convert to `tess` (raw regions with no numbers)
regions <- as.tess(counts)
# Add residuals as marks to the tessellation:
marks(regions) <- test$residuals
# Plot regions with marks as colors:
plot(regions, do.col = TRUE, main = "")
I found a way to convert igraph into visNetwork (refer to Interactive arules with arulesViz and visNetwork). Suppose before and after conversion from igraph to visNetwork should be the same, but my result shows after convert into visNetwork, the results are different.
I'll try demonstrate the issue using sample data data("Groceries") from Library(arules).
#Pre-defined library
library(arules)
library(arulesViz)
library(visNetwork)
library(igraph)
#Get sample data & get association rules
data("Groceries")
rules <- apriori(Groceries, parameter=list(support=0.01, confidence=0.4))
rules <- head(sort(rules, by="lift"), 10)
#Convert rules to data.table
library(data.table)
rules_dt <- data.table( lhs = labels( lhs(rules) ),
rhs = labels( rhs(rules) ),
quality(rules) )[ order(-lift), ]
print all rules in table format (sort by lift)
Plot top 10 association rules via using igraph
ig <- plot(rules, method="graph", control=list(type="items"))
Note: Based on association rules i plot a network diagram via using igraph, everything is correct. Next i'll try to convert existing igraph to visNetwork, then we compare the results.
tf <- tempfile( )
saveAsGraph(rules, file = tf, format = "dot" )
# clean up temp file if desired
#unlink(tf)
# Convert igraph to dataframe
ig_df <- as_data_frame(ig, what = "both")
# Plot visNetwork
visNetwork(
nodes = data.frame(
id = ig_df$vertices$name
,value = ig_df$vertices$lift # could change to lift or confidence
,title = ifelse(ig_df$vertices$label == "",ig_df$vertices$name,
ig_df$vertices$label)
,ig_df$vertices
),
edges = ig_df$edges
) %>%
visEdges(arrows ="to") %>%
visOptions( highlightNearest = T )
Plot top 10 association rules via using visNetwork
Note: For visNetwork diagram, the size of intercept node indicate "lift", the higher the lift, the larger the size of intercept node; unlike igraph diagram, size of intercept node indicate "support", while colour of intercept node indicate "lift".
Let's compare the igraph and visNetwork
By referring to the association rules in table format, rules no.10 (rules with smallest "lift"), suppose the size of the intercept node is the smallest, but end up it's not the smallest.
Problems
I tried to further drill down into ig_df <- get.data.frame( ig, what = "both" ), and i found something weird on ig_df$vertices table, which generated from as_data_frame function from library(igraph).
I found that the assoc10 (intercept node for association rules no.10) actually had NA for all variables (i.e. "support", "confidence", "lift" & "count"), more precisely the dimension for columns "support", "confidence", "lift" & "count" in ig_df$vertices are moving up one row! Kindly correct me if i'm wrong..
Conclusion
Since the key to convert an igraph into visNetwork is to use this as_data_frame to get extract all data from an igraph and convert those data into dataframe, then plot visNetwork using the data from extracted dataframe. But due to extract issue when using as_data_frame to extract data from igraph, so the result from also different.
Question: is this a bug? or i made a mistake on my code? Any suggestion is welcome. Thanks!
A year later... but I already typed everything so might as well.
If I understand it correctly, this is the source of your troubles -
value = ig_df$vertices$lift # could change to lift or confidence
The easiest solution is to assign your lift values to size because in visNetwork size: Number. Default to 25. The size is used to determine the size of node shapes that do not have the label inside of them. These shapes are: image, circularImage, diamond, dot, star, triangle, triangleDown, square and icon. I'm not sure how values works here but I think you could use values if you also provide appropriate scaling.
https://www.rdocumentation.org/packages/visNetwork/versions/2.0.9/topics/visNodes
So the solution is:
size = ig_df$vertices$lift # could change to lift or confidence
Note that the nodes that have NA lift are set to a size 25 by default and that with a size 2 or 3 lift you can barely see the nodes. You could raise the lift size exponentially which will increase the size of the nodes and exaggerate the differences in lift.
ig_df <- as_data_frame(ig, what = "both")
ig_df$vertices["lift"] <- ig_df$vertices["lift"] ** 3
And I couldn't reproduce your shifty table problem, mine looked fine, hopefully it solves itself!
I have hierarchical data in this form:
df <- data.frame(root=rep("unclustered",22),
itr1=paste0("1.",c(1,5,1,2,4,1,3,2,5,5,6,9,4,3,4,8,5,7,3,2,10,8)),
itr2=paste0("2.",c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,10,17,18,19,20,21)),
itr3=paste0("3.",c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)),stringsAsFactors = F)
Which describes iterative cluster assignment of data points - the rows. The first column, root, assigns all points to the root cluster, then each column is an iteration of the clustering which takes the clusters of the previous iteration and breaks them down to further sub-clusters.
I'm trying to plot this process using a tree network.
I know that using data.tree, I can simply do:
df$pathString <- do.call(paste,c(df,sep="/"))
df.graph <- data.tree::as.Node(df)
plot(df.graph)
But I'm looking for something a bit fancier, preferably with a ggplot look.
So I converted df.graph to an igraph object:
df.igraph <- data.tree::as.igraph.Node(df.graph)
And tried to use ggraph:
library(ggraph)
ggraph(df.igraph, 'igraph', algorithm = 'tree') +
geom_edge_link() +
ggforce::theme_no_axes()
Any idea how to get the ggraph option to include the nodes with their labels, add arrows to the edges, and perhaps color each level differently?
This seems to get me close:
V(df.igraph)$class <- names(V(df.igraph))
ggraph(df.igraph,layout='tree')+
geom_edge_link(arrow=arrow(length=unit(2,'mm')),end_cap=circle(3,'mm'))+
geom_node_label(aes(label=class))+
theme_void()
I am making a heatmap, but I can't assign the result in a variable to check the result before plotting. Rstudio plot it automatically. I would like to get the list of rownames in the order of the heatmap. I'am not sure if this is possible. I'am using this code:
hm <- heatmap.2( assay(vsd)[ topVarGenes, ], scale="row",
trace="none", dendrogram="both",
col = colorRampPalette( rev(brewer.pal(9, "RdBu")) )(255),
ColSideColors = c(Controle="gray", Col1.7G2="darkgreen", JG="blue", Mix="orange")[
colData(vsd)$condition ] )
You can assign the plot to an object. The plot will still be drawn in the plot window, however, you'll also get a list with all the data for each plot element. Then you just need to extract the desired plot elements from the list. For example:
library(gplots)
p = heatmap.2(as.matrix(mtcars), dendrogram="both", scale="row")
p is a list with all the elements of the plot.
p # Outputs all the data in the list; lots of output to the console
str(p) # Struture of p; also lots of output to the console
names(p) # Names of all the list elements
p$rowInd # Ordering of the data rows
p$carpet # The heatmap values
You'll see all the other values associated with the dendrogram and the heatmap if you explore the list elements.
To others out there, a more complete description way to capture a matrix representation of the heatmap created by gplots:
matrix_map <- p$carpet
matrix_map <- t(matrix_map)
I am trying to create circular phylogenetic tree. I have this part of code:
fit<- hclust(dist(Data[,-4]), method = "complete", members = NULL)
nclus= 3
color=c('red','blue','green')
color_list=rep(color,nclus/length(color))
clus=cutree(fit,nclus)
plot(as.phylo(fit),type='fan',tip.color=color_list[clus],label.offset=0.2,no.margin=TRUE, cex=0.70, show.node.label = TRUE)
And this is result:
Also I am trying to show label for each node and to color branches. Any suggestion how to do that?
Thanks!
When you say "color branches" I assume you mean color the edges. This seems to work, but I have to think there's a better way.
Using the built-in mtcars dataset here, since you did not provide your data.
plot.fan <- function(hc, nclus=3) {
palette <- c('red','blue','green','orange','black')[1:nclus]
clus <-cutree(hc,nclus)
X <- as.phylo(hc)
edge.clus <- sapply(1:nclus,function(i)max(which(X$edge[,2] %in% which(clus==i))))
order <- order(edge.clus)
edge.clus <- c(min(edge.clus),diff(sort(edge.clus)))
edge.clus <- rep(order,edge.clus)
plot(X,type='fan',
tip.color=palette[clus],edge.color=palette[edge.clus],
label.offset=0.2,no.margin=TRUE, cex=0.70)
}
fit <- hclust(dist(mtcars[,c("mpg","hp","wt","disp")]))
plot.fan(fit,3); plot.fan(fit,5)
Regarding "label the nodes", if you mean label the tips, it looks like you've already done that. If you want different labels, unfortunately, unlike plot.hclust(...) the labels=... argument is rejected. You could experiment with the tiplabels(....) function, but it does not seem to work very well with type="fan". The labels come from the row names of Data, so your best bet IMO is to change the row names prior to clustering.
If you actually mean label the nodes (the connection points between the edges, have a look at nodelabels(...). I don't provide a working example because I can't imagine what labels you would put there.