Get DOT file from a graph when using DiagrammeR - r

It looks like DiagrammeR has changed their create_nodes and create_edges functions than it looks on the doc.
Also the $dot_code attribute is not there anymore. I can't find the replacement for this.
Here is the example code that they had on the doc, but it is not working.
The following example is found in their official DiagrammeR web.
###
# Create a graph with both nodes and edges
# defined, and, add some default attributes
# for nodes and edges
###
library(DiagrammeR)
# Create a node data frame
nodes <-
create_nodes(nodes = c("a", "b", "c", "d"),
label = FALSE,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(3.5, 2.6, 9.4, 2.7))
edges <-
create_edges(from = c("a", "b", "c"),
to = c("d", "c", "a"),
rel = "leading_to")
graph <-
create_graph(nodes_df = nodes,
edges_df = edges,
node_attrs = "fontname = Helvetica",
edge_attrs = c("color = blue",
"arrowsize = 2"))
graph
#> $nodes_df
#> nodes label type style color shape data
#> 1 a lower filled aqua circle 3.5
#> 2 b lower filled aqua circle 2.6
#> 3 c lower filled aqua rectangle 9.4
#> 4 d lower filled aqua rectangle 2.7
#>
#> $edges_df
#> from to rel
#> 1 a d leading_to
#> 2 b c leading_to
#> 3 c a leading_to
#>
#> $graph_attrs
#> [1] NULL
#>
#> $node_attrs
#> [1] "fontname = Helvetica"
#>
#> $edge_attrs
#> [1] "color = blue" "arrowsize = 2"
#>
#> $directed
#> [1] TRUE
#>
#> $dot_code
#> [1] "digraph {\n\ngraph [rankdir = LR]\n\nnode [fontnam...
#>
#> attr(,"class")
#> [1] "dgr_graph"
# View the graph in the RStudio Viewer
render_graph(graph)

look into the help of the DiagrammeR package, the website is indeed not up-to-date.
Look at the function create_graph() in the help that shows how the syntax changed.
something like this should do the same:
###
library(DiagrammeR)
# Create a node data frame (ndf) where the labels
# are equivalent to the node ID values (this is not
# recommended); the `label` and `type` node
# attributes will always be a `character` class
# whereas `id` will always be an `integer`
nodes <-
create_node_df(
n = 4,
type = "lower",
label = c("a", "b", "c", "d"),
style = "filled",
fillcolor = "blue",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(3.5, 2.6, 9.4, 2.7))
# Create an edf with additional edge
# attributes (where their classes will
# be inferred from the input vectors)
edges <-
create_edge_df(
from = c(1, 2, 3),
to = c(4, 3, 1),
rel = "leading_to")
# With `create_graph()` we can
# simply create an empty graph (and
# add in nodes and edges later
# with other functions)
graph <-
create_graph(
nodes_df = nodes,
edges_df = edges) %>%
set_edge_attrs(
edge_attr = color,
values = "green") %>%
set_edge_attrs(
edge_attr = arrowsize,
values = 2) %>%
set_node_attrs(
node_attr = fontname,
values = "Helvetica")
graph
render_graph(graph)

The Bad News
For anyone viewing this zombie thread, as of this posting, the information posted on Rich Iannone's documentation page for DiagrammeR is still out of date: http://rich-iannone.github.io/DiagrammeR/graph_creation.html.
The Good News
However, the vignettes for the Diagrammer package contain updated information. As shown in the package vignette named Creating Simple Graphs from NDFs/EDFs, you can view and export GraphViz dot code with a function named generate_dot().

Related

Is there any way to customize legend histogram using tm_layout

Please see the map I drew below using the tmap package. I did not any find parameters that I can use to customize the font of the histogram legend. From the code below, you can see that I've already set the legend.text.fontface = 'bold'. However, this did not work.
psp1 <- tm_shape(province) +
tm_borders(col = 'black') +
tm_shape(county) +
tm_polygons(col = '+1 °C', title = 'Changes in %', style = 'pretty', aes.palette = 'div', n=5, legend.hist = T) +
tm_compass(north = 0, type = 'arrow', show.labels =0, position = c('right','top')) +
tm_layout(legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
legend.outside = T, legend.outside.position = 'bottom',
legend.hist.width = 1,
legend.hist.height = 0.5,
legend.stack = 'horizontal',
legend.title.fontface = 'bold',
legend.text.fontface = 'bold')
Very interesting question. Indeed, it does not seem possible to change the font of the labels for the histogram using legend.text.fontface = 'bold'
Hopefully, it is possible to change this using the base R library grid on which the tmap library is based on.
So, please find below one possible solution to your request (hoping that this answer does not come too late and that it will still be useful to you)
Preliminary note for other Stackoverflow users: to run the reprex below correctly you will need to first download the data made available by the OP in this post.
Reprex
STEP 1 - BUILDING THE MAP WITH THE LEGEND
library(sf)
library(tmap)
library(RColorBrewer)
setwd("Add the path to your working directory")
# Import data
province <- st_read("province.shp")
county <- st_read("county.shp")
# Split the 'sf' object 'county' into a list of five 'sf' objects
county_warm_list <- split(county , f = county$warming)
# Build the map with the legend
psp1 <- tm_shape(province) +
tm_borders(col = 'black') +
tm_shape(st_sf(county_warm_list[[3]])) + # using the scenario +3°C
tm_polygons(col = 'estimate',
title = 'Changes in %',
style = 'pretty',
aes.palette = 'div',
n=5,
legend.hist = TRUE,
midpoint = 0) +
tm_compass(north = 0,
type = 'arrow',
show.labels =0,
position = c('right','top')) +
tm_layout(legend.show = TRUE,
legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
legend.outside = TRUE,
legend.outside.position = 'bottom',
legend.hist.width = 1,
legend.hist.height = 0.5,
legend.stack = 'horizontal',
legend.title.fontface = 'bold',
legend.text.fontface = 'bold')
STEP 2 - BOLD ALL THE LABELS IN THE LEGEND (i.e. including those in the histogram)
library(grid)
# Convert the 'tmap' object psp1 into a 'grob' object ('grob' = 'grid graphical object')
psp1 <- tmap_grob(psp1)
# Find the name of the element we want to change using 'grid.list()' which
# returns a listing of 'grobs' (including gTree)
grid.ls(psp1)
#> GRID.gTree.41
#> multiple_1
#> BG
#> mapBG
#> mapElements
#> GRID.gTree.11
#> tm_polygons_1_2
#> GRID.gTree.12
#> tm_polygons_1_3
#> GRID.rect.13
#> meta_with_bg
#> meta
#> GRID.gTree.16
#> GRID.gTree.15
#> compass
#> GRID.polygon.14
#> outside_legend !!!! "outside_legend" element !!!!
#> meta_with_bg
#> meta
#> legend
#> GRID.rect.39
#> GRID.gTree.40
#> GRID.gTree.19
#> GRID.gTree.18
#> GRID.text.17
#> GRID.gTree.23
#> GRID.gTree.22
#> GRID.rect.20
#> GRID.text.21
#> GRID.gTree.38
#> GRID.gTree.37
#> GRID.gTree.36
#> GRID.gTree.25
#> GRID.rect.24
#> GRID.gTree.27
#> GRID.polyline.26
#> GRID.gTree.29
#> GRID.text.28
#> GRID.gTree.33
#> GRID.gTree.30
#> GRID.lines.31
#> GRID.polyline.32
#> GRID.gTree.35
#> GRID.text.34
In the listing of grob objects just above, you can see an element named "outside_legend". So, we will modify it to bold the fonts of the legend:
# Edit the 'outside_legend' element of the 'grob' object 'psp1' using
# 'editGrob()' and save it in the new 'grob' object 'my_map'
my_map <- editGrob(psp1, gPath("outside_legend"), gp = gpar(fontface = "bold"))
# Draw the 'grob' object 'my_map'
# !!!! NB: may take a few seconds to be displayed in the graphic device !!!!
grid.draw(my_map)
STEP 3 - SAVING THE MAP EITHER MANUALLY OR PROGRAMMATICALLY
(in the latter case, you need to install the rstudioapi library)
rstudioapi::savePlotAsImage(
"my_map.png", # add the path if different of the working directory
format = "png", # other possible formats: "jpeg", "bmp", "tiff", "emf", "svg", "eps"
width = 670,
height = 710
)
And that's it :-)
Created on 2022-01-30 by the reprex package (v2.0.1)

Thin gray lines as links plotting sankeyNetwork in R

Disclaimer: I'm an R newbie, so I may be overlooking something really obvious here...
I am currently working on a sankeyNetwork diagram using R, and I am facing a problem that almost seems to be a bug, but I'm completely clueless...
I've googled extensively, and haven't been able to find anybody else reporting the same...
The problem is that in my code I currently have 7 nodes, and 5 links. When I plot the diagram, everything works fine:
Plot 1, everything working fine
This is the code for Plot 1:
library(networkD3)
# List of nodes (portfolios & targets)
nodes = data.frame("trialnodes" =
c("portfolio1", # 0
"portfolio2", # 1
"portfolio3", # 2
"portfolio4", # 3
"target1", # 4
"target2", # 5
"target3" # 6
))
# List of links
links = as.data.frame(matrix(c(
0,4,2,
1,6,1,
2,3,1,
2,6,1,
3,5,1),
byrow = TRUE, ncol = 3))
# Column names of data frame
names(links) = c("source", "target", "value")
# check
links
# Sankey Diagram
# Colour scale
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);")
# Diagram
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target", Value = "value", NodeID = "trialnodes",
fontSize = 14, nodeWidth = 10, nodePadding = 140, iterations = 0,
colourScale = colourScale)
however, as soon as I add one more node, and 1 more link, the plot's format becomes completely broken, showing the links between nodes as simple gray thin lines (not representing the Value). The nodeWidth and nodePadding specifications also get ignored :(
Plot 2, links as thin gray lines
This is the code for Plot 2:
library(networkD3)
# List of nodes (portfolios & targets)
nodes = data.frame("trialnodes" =
c("portfolio1", # 0
"portfolio2", # 1
"portfolio3", # 2
"portfolio4", # 3
"target1", # 4
"target2", # 5
"target3", # 6
"target4" # 7
))
# List of links
links = as.data.frame(matrix(c(
0,4,2,
0,7,1,
1,6,1,
2,3,1,
2,6,1,
3,5,1),
byrow = TRUE, ncol = 3))
# Column names of data frame
names(links) = c("source", "target", "value")
# check
links
# Sankey Diagram
# Colour scale
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);")
# Diagram
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target", Value = "value", NodeID = "trialnodes",
fontSize = 14, nodeWidth = 10, nodePadding = 140, iterations = 0,
colourScale = colourScale)
Can anybody spot what's going on? I hope someone can help... I'm desperate D: Thank you very much in advance! :)
Either reduce your nodePadding value to something reasonable, or make the viewer/browser-window size large enough to show the max number of nodes you have in a column * 140 pixels (plus some for the actual node) and then refresh (in your second example that comes out to ~600px).
library(networkD3)
# List of nodes (portfolios & targets)
nodes = data.frame("trialnodes" =
c("portfolio1", # 0
"portfolio2", # 1
"portfolio3", # 2
"portfolio4", # 3
"target1", # 4
"target2", # 5
"target3", # 6
"target4" # 7
))
# List of links
links = as.data.frame(matrix(c(
0,4,2,
0,7,1,
1,6,1,
2,3,1,
2,6,1,
3,5,1),
byrow = TRUE, ncol = 3))
# Column names of data frame
names(links) = c("source", "target", "value")
# check
links
# Sankey Diagram
# Colour scale
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);")
# Diagram
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target", Value = "value", NodeID = "trialnodes",
fontSize = 14, nodeWidth = 10, nodePadding = 14, iterations = 0,
colourScale = colourScale)

How to construct bipartite graphs using igraph? [duplicate]

This question already has an answer here:
How to create a bipartite network in R with igraph or tnet
(1 answer)
Closed 5 years ago.
I need to create a bipartite graph for consumer-brand relationships.
This is my example data:
datf <- data.frame(Consumers = c("A", "B", "C", "D", "E"),
Brands = c("Costa", "Starbucks", "Cafe2U", "Costa", "Costa"))
The following code gives me a network. But I am not sure how I can add a node type attribute to label consumers and brands:
library(igraph)
dat=read.csv(file.choose(),header=TRUE)
el=as.matrix(dat)
el[,1]=as.character(el[,1])
el[,2]=as.character(el[,2])
g=graph.edgelist(el,directed=FALSE)
I would like to create a bipartite graph with edges that connect each consumer with the brand they like. Ideally, the nodes will be labeled with text.
Could you show me how to do this using library(igraph)?
This resource at Shizuka Lab is really useful for exploring bipartite networks in R with igraph. In short:
library(igraph)
# Your matrix containing consumer choice by brands
m = matrix(data = sample(0:1, 25, replace = TRUE), nrow = 5, ncol = 5)
colnames(m) = c("A", "B", "C", "D", "E")
rownames(m) = c("Costa", "Starbucks", "Cafe2U", "Petes", "Philz")
# Convert it to a bipartitie network
bg = igraph::graph.incidence(m)
bg
# See the vertex attributes
V(bg)$type
V(bg)$name
# Plot the network
shape = ifelse(V(bg)$type, "circle", "square") # assign shape by node type
col = ifelse(V(bg)$type, "red", "yellow") # assign color by node type
plot(bg, vertex.shape = shape, vertex.color = col)
Gives:

How arrange diagram in R

I want to get diagram similar to picture below, but code I use creates different diagram. With rbind I added some hierarchy to a diagram. In data frame col0 there is a string with names of animals. In col1 string is split into individual animals & col2 is adding latin name for a animal. col1 data are always changing and in col2 data constant (there always be feline or canis names in that column).
library(igraph)
# I create my dataframe with animals
df <- data.frame(col0 = c("Cat Dog Wolf", "Cat Dog Wolf", "Cat Dog Wolf"),
col1 = c( "Cat", "Dog", "Wolf"),
col2 = c( "Feline", "Canis", "Canis2"))
# Add extra lines for hierarchy
# These lines work with current graph for a new one these should be replace or deleted
df <-rbind(df, data.frame(col0 = "Cat Dog Wolf", col1 = "Feline", col2 ="Animal"))
df <-rbind(df, data.frame(col0 = "Cat Dog Wolf", col1 = "Canis", col2 = "Animal"))
df <-rbind(df, data.frame(col0 = "Cat Dog Wolf", col1 = "Canis2", col2 = "Canis"))
##########
df <-df[c('col2', 'col1')]
names(df) <-c('from', 'to')
abc <-union(df$to, df$from)
###########
g <-graph.data.frame(df, directed = TRUE, vertices = abc)
plot(g, vertex.size = 20, vertex.label.dist = 0.5, vertex.color = c("blue",
"red", "green", "white", "orange" ),
edge.arrow.size = 0.5, layout = layout.reingold.tilford(g))
This is the graph that the above code outputs, but it's not quite what I want:
I want a similar diagram to what's shown below:
I think that I understand what you want, but I will restate the problem
so that you can confirm whether or not I understood. I think that what
you want to do is this:
Find all of the leaves in the tree, i.e. the nodes with no descendants.
Each leaf will have one parent. Rename the parent with the name of the
leaf, then delete the leaf from the graph. The following code implements that.
## Assume that we have created the graph g using your code
g2 = g # Keep original graph intact
SourceNodes = sapply(strsplit(attr(E(g2), "vnames"), "\\|"), "[", 1)
DestNodes = sapply(strsplit(attr(E(g2), "vnames"), "\\|"), "[", 2)
## Leaf nodes are nodes that are destinations, but not sources
## Also need the node numbers for later deletion
(LeafNodes = DestNodes[which(!(DestNodes%in% SourceNodes ))])
[1] "Cat" "Dog" "Wolf"
(LeafNumbers = match(LeafNodes, attr(V(g), "name")))
[1] 1 2 3
## Find the parents of the leaves
(UpOne = SourceNodes[match(LeafNodes, DestNodes)])
[1] "Feline" "Canis" "Canis2"
## Rename the UpOne nodes (parents of leaves)
vertex_attr(g2)$name[match(UpOne, vertex_attr(g2)$name)] = LeafNodes
## Now delete the leaf nodes and plot
g2 = delete_vertices(g2, LeafNumbers)
plot(g2, vertex.size = 20, vertex.label.dist = 0.5,
vertex.color = c("red", "green", "white", "orange" ),
edge.arrow.size = 0.5, layout = layout.reingold.tilford(g2))
Result

Keeping the label order on the y-axis when using seqpcplot in TraMineR

I'm using the R package TraMineR. I would like to plot frequent event sequences by using the command seqpcplot. I previously coded the states in the alphabet as to keep them in alphabetical order so that when I compute the sequences by using the seqdef command without specifying the labels and states options I obtain the following output:
[>] state coding:
[alphabet] [label] [long label]
1 a.sin a.sin a.sin
2 b.co0 b.co0 b.co0
3 c.co1 c.co1 c.co1
4 d.co2+ d.co2+ d.co2+
5 e.ma0 e.ma0 e.ma0
6 f.ma1 f.ma1 f.ma1
7 g.ma2+ g.ma2+ g.ma2+
8 h.sin0 h.sin0 h.sin0
9 i.lp1 i.lp1 i.lp1
10 l.lp2+ l.lp2+ l.lp2+
11 m.lp1_18 m.lp1_18 m.lp1_18
12 n.lp2_18 n.lp2_18 n.lp2_18
I then convert the state-sequence objet in an event-sequece objet by using seqecreate. When plotting the event sequences by seqpcplot I obtain a very nice graph where the states are ordered alphabetically on the y-axis according to the alphabet.
However, I would like to use longer labels in the graphs, so that I specified the labels and states options in the seqdef command as
lab<-c("single", "cohabNOchildren","cohab1child","cohab2+children","marrNOchildren","marr1child","marr2+children","singleNOchildren","loneMother1child","loneMother2+children","loneMother1child_over18","loneMother2+children_over18")
obtaining:
[>] state coding:
[alphabet] [label] [long label]
1 a.sin single single
2 b.co0 cohabNOchildren cohabNOchildren
3 c.co1 cohab1child cohab1child
4 d.co2+ cohab2+children cohab2+children
5 e.ma0 marrNOchildren marrNOchildren
6 f.ma1 marr1child marr1child
7 g.ma2+ marr2+children marr2+children
8 h.sin0 singleNOchildren singleNOchildren
9 i.lp1 loneMother1child loneMother1child
10 l.lp2+ loneMother2+children loneMother2+children
11 m.lp1_18 loneMother1child_over18 loneMother1child_over18
12 n.lp2_18 loneMother2+children_over18 loneMother2+children_over18
As before, I then computed the event sequences and plot them by using seqpcplot:
seqpcplot(example.seqe,
filter = list(type = "function",
value = "cumfreq",
level = 0.8),
order.align = "last",
ltype = "non-embeddable",
cex = 1.5, lwd = .9,
lcourse = "downwards")
This time the states on the y-axis were the states are ordered alphabetically but following the order given by the labels and states labels rather than the alphabet, as I wished.
Is there a way to keep the alphabetical order given in the alphabet when plotting with seqpcplot when the labels and states options are specified and may follow a different alphabetical order from the alphabet?
Thanks.
I agree with the solution above. As a supplement, here a number of possible solutions:
Using seqecreate and the alphabet argument in seqpcplot:
dat <- data.frame(id = factor(1, 1, 1),
timestamp = c(0, 20, 22),
event = factor(c("A", "B", "C")))
dat.seqe <- seqecreate(dat)
seqpcplot(dat.seqe, alphabet = c("C", "A", "B"))
Using seqecreate only
dat <- data.frame(id = factor(1, 1, 1),
timestamp = c(0, 20, 22),
event = factor(c("A", "B", "C"),levels = c("C", "A", "B")))
dat.seqe <- seqecreate(dat)
seqpcplot(dat.seqe)
Using seqdef (here the original categories are different than the labels to be shown in the y-axis)
dat <- data.frame(id = factor(1),
ev.0 = factor("AA", levels = c("CC", "AA", "BB")),
ev.20 = factor("BB", levels = c("CC", "AA", "BB")),
ev.22 = factor("CC", levels = c("CC", "AA", "BB")))
dat.seq <- seqdef(dat, var = 2:4, alphabet = c("CC", "AA", "BB"),
states = c("C", "A", "B"))
seqpcplot(dat.seq)
The last solution may be the one you're looking for. Hope it helps.
The alphabet argument of the seqpcplot function is there to control that order. Something like
seqpcplot(example.seqe,
alphabet = lab,
filter = list(type = "function",
value = "cumfreq",
level = 0.8),
order.align = "last",
ltype = "non-embeddable",
cex = 1.5, lwd = .9,
lcourse = "downwards")
should give you the expected plot.

Resources