Converting a class of sankeyNetwork into a grob - r

I am trying to make a panel plots of 3 sankey diagram in R using networkD3 library. The Sankey diagrams are well generated and I have assigned three variables (p1, p2 and p3) to the plots. Now I want to combine them using ggarrange into one single plot using the code below;
ggarrange(p1, p2, p3, labels = c("A", "B", "C"), ncol = 1, nrow = 3)
But, I keep getting the following error.
Warning message:
In as_grob.default(plot) :
Cannot convert object of class sankeyNetworkhtmlwidget into a grob.
Apparently, ggarrange does not accept the format of my plots. How can I convert them into a format that ggarange will accept.
Here is the code that I used to generate the final Sankey Diagram for the first one (p1). The rest two (p2, p3) are generated using the same code.
nodes = data.frame("name" = c("Rocket 1", "Kerosene","Carbon IV Oxide",
"Aluminium IV Oxide","Sulphur IV Oxide"))
links = as.data.frame(matrix(c(0, 1, 488.4, 1, 2, 576.7, 1, 3, 24.4,
1, 4, 0.3), byrow = TRUE, ncol = 3))
p1 = names(links) = c("source", "target", "value")
p1 = sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
fontSize= 12, nodeWidth = 20)
I would really appreciate some help with this. Thank you.

Related

R igraph cluster nodes with the same colour (feature)

# example data
library(igraph)
links <- cbind.data.frame(from = rep("A", 6),
to = LETTERS[1:6],
weight = rep((1:3), each =2))
nodes <- nodes <- cbind.data.frame(id = LETTERS[1:6],
feature = rep((1:3), each =2))
net <- graph_from_data_frame(d = links, vertices = nodes, directed = T)
V(net)$color <- V(net)$feature
plot(net, vertex.size=30, edge.arrow.size = 0)
This is what I get:
What I want is to cluster the same colored nodes together, something similar as shown in the figure below. How can I do it?
Maybe the option mark.groups in plot could help
plot(net,mark.groups = split(V(net)$name,V(net)$color))
which gives

sankeyD3 > sankeyNetwork implementation of 'NodePosX'

I have been using the sankeyD3 package to create SankeyNetworks and the 'NodePosX' feature isn't working for me yet. The 'NodePosX' feature is not in the 'networkD3' package but it is in the 'sankeyD3' package.
To help illustrate the problem that I am having, I have edited the example from akraemer007 that was posted here to include the X positions of the nodes (see below) but it's still not working in the way that he had originally wanted, with manual control over the x-position of the 'Opted-Out' node.
We're aiming for something like this, but without the small line from 'Opted-Out' to 'Activated':
library(devtools)
devtools::install_github("fbreitwieser/sankeyD3")
library(sankeyD3)
name <- c('Enrolled', 'Opted-Out', 'Invited', 'Activated')
xpos <- c(0, 1, 1, 2)
nodes <- data.frame(name, xpos)
source <- c(0, 0, 2, 1)
target <- c(1, 2, 3, 3)
value <- c(20, 80, 60, 0)
links <- data.frame(source, target, value)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",NodePosX = "xpos",
units = "TWh", fontSize = 12, nodeWidth = 30)
Assuming the last row in your links data frame is only there to force the plot to look the way you want and not part of the actual data you want to plot, you can achieve this with networkd3 using the sinksRight = FALSE parameter.
library(networkD3)
name <- c('Enrolled', 'Opted-Out', 'Invited', 'Activated')
xpos <- c(0, 1, 1, 2)
nodes <- data.frame(name, xpos)
source <- c(0, 0, 2)
target <- c(1, 2, 3)
value <- c(20, 80, 60)
links <- data.frame(source, target, value)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
units = "TWh", fontSize = 12, nodeWidth = 30, sinksRight = FALSE)

Printing Venn Diagram after calculating overlap

I'm trying to use the calculate.overlap function within the VennDiagram package to first calculate and then print a Venn Diagram. I was able to calculate the overlap of my data set but looking for help how to print the Venn graphic. Can anyone provide assistance? I read through the documentation but didn't find this.
> library('VennDiagram')
# A simple single-set diagram
cardiome <- letters[1:10]
superset <- letters[8:24]
overlap <- calculate.overlap(
x = list(
"Cardiome" = cardiome,
"SuperSet" = superset
)
);
Another simple example that shows how to print a Venn diagram using the VennDiagram package:
library(VennDiagram)
cardiome <- letters[1:10]
superset <- letters[8:24]
overlap <- calculate.overlap(
x <- list("Cardiome"=cardiome, "SuperSet"=superset))
venn.plot <- draw.pairwise.venn(
area1 = length(cardiome),
area2 = length(superset),
cross.area = length(overlap),
category = c("Cardiome", "Superset"),
fill = c("blue", "red"),
lty = "blank",
cex = 2,
cat.cex = 2,
cat.pos = c(180, 180),
cat.dist = 0.05,
cat.just = list(c(0, 1), c(1, 1))
)
grid.draw(venn.plot)
savePlot(filename="venndiag", type="png")
Venn diagrams with item labels inside the sets:
library(RAM)
vectors <- list(Cardiome=cardiome, Superset=superset)
group.venn(vectors=vectors, label=TRUE,
fill = c("blue", "red"),
cat.pos = c(180, 180),
lab.cex=1.1)
The funtion venn.diagram() does it. For instance in your example
venn.diagram(x = list(
"Cardiome" = cardiome,
"SuperSet" = superset
), "plot_venn")
It saves to working directory. Type getwd() to see what it is set to.
See the
?venn.diagram()
for more info.
?venn.diagram suggests this
library('VennDiagram')
venn.plot <- venn.diagram(
x = list(
cardiome = letters[1:10],
superset = letters[8:24]
),
filename = NULL
);
grid.draw(venn.plot);

How to create a massive tree diagram in RStudio?

I am new to R.
I want to create a massive tree diagram that represent a Lotto game in R that looks like Tree Diagram 1 in this picture(I made it via PowerPoint):
The problem is I need to draw 6 balls out of 45 balls. The totally elements in this case will be 127. I tried to create a tree diagram using PowerPoint and it looks like Tree Diagram 2.
Then I gave up. I can't type "match' and "no match" and calculate the probability manually for such a massive diagram.
How can I create a tree diagram that looks like Tree Diagram 2 that has similar labels in Tree Diagram 1?
The DiagrammeR package should be helpful:
library(DiagrammeR)
nodes <- create_nodes(nodes = 1:7, type = "number")
edges <- create_edges(from = c(1, 1, 2, 2, 3, 3),
to = c(2, 3, 4, 5, 6, 7),
rel = "leading to")
graph <- create_graph(nodes_df = nodes,
edges_df = edges,
graph_attrs = "layout = dot",
node_attrs = "fontname = Helvetica",
edge_attrs = "color = gray20")
# View the graph
render_graph(graph)
You can get "fancy" with the programming and labels accordingly:
nodes <- create_nodes(nodes = 1:7, type = "number",
label = c("Lotto", rep(c("match", "no match"), times = 3)))
### Same Code as Above...

Cluster annotations using pheatmap

So, I am trying to create a pretty heatmap with pheatmap function in R.
I want to have coloured bars where the dendrogram stops and the graph starts in order to annotate the different clusters. Like a horizontal thick line running through columns and changes colours when it pass to another cluster. It's pretty common I think. But I can't figure out the 'annotations' in pheatmap. Any ideas?
structure<-matrix(rnorm(10000),ncol=100,nrow=100)
dist_structue<-dist(structure)
clustering<-hclust(dist)
cols2 = colorRampPalette(c('green','white','red'))(20)
annotation <- data.frame(Var1 = factor(cutree(clustering, k = 4))
heat_chem <- pheatmap(as.matrix(dist_structure),
clustering_distance_rows=dist_structure,
cellwidth = 4, cellheight = 4, cluster_rows=T,cluster_cols=T, legend = T,
clustering_distance_cols = dist_structure, show_rownames = TRUE,
fontsize_row = 5, annotation_row = annotation,
show_colnames = FALSE, cutree_cols = 4, cutree_rows=4,
color = cols2,
main = 'Heatmap')
The rownames of annotation are not matching the rownames of as.matrix(dist_structure)

Resources