I am trying to complete a network analysis using the function gplot in the package sna. I created an attribute file that includes a column listing the labels I want to show in the figure instead of the NodeName.
VP$Buyer_Relationships$Attributes<- data.frame (NodeName = c(P12, P14, P15, P16), label = c("Bodega", "Bragg", "Cruz", "Angeles"))
sna::gplot(VP$Buyer_Relationships,
gmode="twomode", edge.lwd=edgeweight,
displaylabels=TRUE, label=VP$Buyer_Relationships$Attributes$label, boxed.labels = TRUE
)
But, the figure has labeled the nodes incorrectly. I believe this is because it "defaults to the vertex index number." How do I make it so the nodes show the label I associated with that NodeName?
Related
I am using R "phytools" library for the 3D phylogenetic tree.
And I have made 3D tree successfully with the script below
library(phytools)
library(rgl)
tree<-read.tree(text="((un6:3,(un2:2,(un7:1,un5:1):1):1):1,((un1:1,un4:1):1,un3:1,un8:1):1):1,(un9:2,un10:2):1):1):1;")
class(tree) <- "phylo"
Bae2 <- matrix(c(1,2,2,3,3,0.5,4,4,5,5,1,4,6,7,9,2,1,1,7,8),10,2,byrow=TRUE)
rownames(Bae2) <- c("un1","un2","un3","un4","un5","un6","un7","un8","un9","un10")
fancyTree(tree, type="traitgram3d",X=Bae2,control=list(spin=TRUE)
The output is
What I want to do is, since the tree is in 3D, branches are hard to distinguish from each other.
So is there any way I can add colors to the branches?
Exploring the function fancyTree at github, we can see that the argument type="traitgram3d" internally calls a function traitgram3d, which in turn is a wrapper for function phylomorphospace3d. Typing
?phylomorphospace3d
gives explanation for the argument control as
col.edge a vector of colors of length nrow(tree$edge).
Let's choose some colors from a Set 3 and use them to color the tree edges.
cols <- hcl.colors(nrow(tree$edge), palette = "Set 3")
fancyTree(tree, type = "traitgram3d",X = Bae2,
control = list(col.edge = cols))
I have applied DBSCAN algorithm on built-in dataset iris in R. But I am getting error when tried to visualise the output using the plot( ).
Following is my code.
library(fpc)
library(dbscan)
data("iris")
head(iris,2)
data1 <- iris[,1:4]
head(data1,2)
set.seed(220)
db <- dbscan(data1,eps = 0.45,minPts = 5)
table(db$cluster,iris$Species)
plot(db,data1,main = 'DBSCAN')
Error: Error in axis(side = side, at = at, labels = labels, ...) :
invalid value specified for graphical parameter "pch"
How to rectify this error?
I have a suggestion below, but first I see two issues:
You're loading two packages, fpc and dbscan, both of which have different functions named dbscan(). This could create tricky bugs later (e.g. if you change the order in which you load the packages, different functions will be run).
It's not clear what you're trying to plot, either what the x- or y-axes should be or the type of plot. The function plot() generally takes a vector of values for the x-axis and another for the y-axis (although not always, consult ?plot), but here you're passing it a data.frame and a dbscan object, and it doesn't know how to handle it.
Here's one way of approaching it, using ggplot() to make a scatterplot, and dplyr for some convenience functions:
# load our packages
# note: only loading dbscacn, not loading fpc since we're not using it
library(dbscan)
library(ggplot2)
library(dplyr)
# run dbscan::dbscan() on the first four columns of iris
db <- dbscan::dbscan(iris[,1:4],eps = 0.45,minPts = 5)
# create a new data frame by binding the derived clusters to the original data
# this keeps our input and output in the same dataframe for ease of reference
data2 <- bind_cols(iris, cluster = factor(db$cluster))
# make a table to confirm it gives the same results as the original code
table(data2$cluster, data2$Species)
# using ggplot, make a point plot with "jitter" so each point is visible
# x-axis is species, y-axis is cluster, also coloured according to cluster
ggplot(data2) +
geom_point(mapping = aes(x=Species, y = cluster, colour = cluster),
position = "jitter") +
labs(title = "DBSCAN")
Here's the image it generates:
If you're looking for something else, please be more specific about what the final plot should look like.
In order to create an Hasse Diagram like the following
One is using the following libraries
library(rPref)
library(Rgraphviz)
One is taking a small sample of one's data
df <- data[1:10,]
Then creating the preferences
pref <- low(time) * low(MAPE)
And the Better-Than-Graph (BTG)
btg <- get_btg(df, pref)
In order to display the labels for the nodes containing relevant values, one is creating the labels as following
labels <- paste0(df$time, "\n", df$MAPE)
However, when one builds the visualization with
plot_btg(df, pref, labels)
One can only see the first label, instead of the two. Here is what one is seeing
Passing use_dot=FALSE solved the problem
plot_btg(df, pref, labels, use_dot = FALSE)
Can I arrange all my study labels within subgroups in my forest plot with the year of publication after specifying that I want subgroups divided by a certain variable?
Here is the code I am currently using.
brugia.forest <- metaprop(event = no.positive, n = no.tested, studlab = studylabel, data = brugia, byvar = diagnostics, bylab = c("direct detection", "direct and indirect detection", "indirect detection"), print.byvar = F, sm = "PLO", method.tau = "REML", title = "", hakn = T)
I would like the studies within the "diagnostics" groups to be arranged from the oldest to the most recent and not alphabetically as is currently the case. I am using the meta package of R because of its user-friendliness and would like to continue using it (so, metafor suggestions may not be too helpful)
Thanks.
I would like to answer this question because the creator of the meta package, Dr. Guido Schwarzer was kind to answer the question via email. Here is the way forward:
By default, the forest function does not sort the studies at all,
instead the order of the dataset is used. One can therefore order the dataset before utilising it for the forestplot first.
Alternatively, one can use the 'sortvar' function to change the order of studies and specify the variable one wants to sort the studies by.
Hope this helps.
Another option is to use ggplot if you are familiar with the ggplot package. Gives a lot of flexibility in arranging and modifying the plots
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!