DiagrammeR: How to insert a line break within a node? - r

I want to create a flowchart with the R package DiagrammeR. The text of some of the nodes should have a line break.
Consider the following reproducible example in R:
library("DiagrammeR")
# Create a node data frame (ndf)
ndf <- create_node_df(n = 4,
label = c("hi stacko", "aaa", "bbb", "ccc"))
# Create an edge data frame (edf)
edf <- create_edge_df(from = c(1, 2, 3, 3),
to = c(4, 3, 1, 4))
# Create a graph with the ndf and edf
graph <- create_graph(nodes_df = ndf,
edges_df = edf)
# Create a PDF file for the graph (`graph.pdf`)
graph %>%
render_graph()
In this flowchart, I would like to add a line break between "hi" and "stacko" in the lower left node. I found some sources that suggested <br> or \n. Unfortunately, both did not work.
Question: How could I insert a line break in DiagrammeR?

This works for me:
ndf <- create_node_df(n = 4,label = c("hi\nstacko", "aaa", "bbb", "ccc"))
and, when run with the remainder of the code, produces the following diagram:

Related

How to zoom in on/extract a subsection of/extract colours from a heatmap.2 in R?

I'm creating heatmaps in R using heatmap.2 (I think it needs to be heatmap.2 because I'm using 1 dataset to generate the colours of the heatmap and a second dataset to overlay numerical data).
Here is a sample of my code so far. The actual data set is 30 columns and 1000 rows.
heatmap_all_data <-
data.frame(name = c("John", "Mark", "Luke", "Jack", "Will", "Jim", "Clive", "Steve"),
trait_1 = c(1, 2, 5, 8, 5, 3, 7, 8),
trait_2 = c(5, 7, 3, 4, 6, 3, 2, 1)) %>%
column_to_rownames(var="name")
heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)
heatmap.2(as.matrix(heatmap_all_data),
scale = "column",
key = FALSE,
dendrogram = "none",
Rowv = FALSE,
Colv = FALSE,
trace = "none",
col = rev(heatmap_colour),
labRow = row.names(heatmap_all_data))
Which generates the following heatmap: https://i.stack.imgur.com/lK8Sc.png
NOW, the problem is I only want a subsection of this data, e.g I want the following heatmap:
heatmap_part_data <-
data.frame(name = c("John", "Mark", "Luke"),
trait_1 = c(1, 2, 5),
trait_2 = c(5, 7, 3)) %>%
column_to_rownames(var="name")
heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)
heatmap.2(as.matrix(heatmap_part_data),
scale = "column",
key = FALSE,
dendrogram = "none",
Rowv = FALSE,
Colv = FALSE,
trace = "none",
col = rev(heatmap_colour),
labRow = row.names(heatmap_part_data))
https://i.stack.imgur.com/j33Ic.png
BUT, I want each cell to keep the same colours as the original. I.e. I want the colours in my subsetted heatmap to be relative to the total data and not just the subsetted data. (In the real example I want to show 10 out of 1000 entries).
So, I need to either "zoom in" and rescale the top section of the heatmap and then crop the image, extract the top section of the heatmap into a new object while maintaining the same colours, or extract information about the colours in the full heatmap and overwrite the default colours in the subsetted heatmap.
The goal is basically to output an image of the subsetted data heatmap with each colour in each cell the same as in the all_data heatmap.
I hope this is clear - please advise if you need any clarification!
Many thanks for taking the time to read and I hope someone can help.
Best,
Ryan
Found the solution!
So I switched from heatmap.2 to heatmaply - same functionality but with interactivity. With heatmaply you can drag an area over the heatmap and zoom into that area which gives the desired result but I wanted to consistently zoom to a specific area.
From this website (https://plotly.com/r/axes/) I found out about the Layout function of the wider plotly library (that heatmaply is part of).
So to the existing code you can add:
%>% layout(yaxis = list(range = c(10.5, 0.5)))
(Need to add 0.5 to centre the rows properly)
Et voila! The heatmap colours are generated relative to the wider dataset but only a subset is shown.

Show edge attributes as label with igraph

I am using igraph in R for network analysis. I want to display an edge attribute on each line in the plot. An example is below
df <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
nod <- data.frame(node = c(0:7),wt = c(1:8))
pg <- graph_from_data_frame(d = df, vertices = nod,directed = F)
plot(pg)
I want the value of the "wt" feature to show up between each node on the line, or preferably, in a little gap where the line breaks.
Is it possible to make this happen?
Use the parameter edge.label to assign labels of the edges, I used - probably wrong - nod$wt. Of course, you could assign other labels.
You could use the following code:
# load the package
library(igraph)
# your code
df <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
nod <- data.frame(node = c(0:7),wt = c(1:8))
pg <- graph_from_data_frame(d = df, vertices = nod,directed = F)
# plot function with edge.label added
plot(pg, edge.label = nod$wt)
Please, let me know whether this is what you want.

customized legends by data points in r dygraphs

I understand using dySeries function from the R dygraphs package can customize the overall legend text. However, I wonder how to create customized legend text in dygraphs (or other tricks to reach the goal). As an example:
library(dygraphs)
data <- data.frame(game = c(1:5),
oppnt = c("aaa", "bbb", "ccc", "ddd", "eee"),
score = c(10, 20, 15, 12, 4))
data %>% select(game, score) %>% dygraph() %>%
dySeries("score", label = "X's score")
This will show X's score: (interactive score) when mouseover each data point. How can I show information like Opponent XXX, X's score: (interactive score). For example: "Opponent ccc, X's score: 15"? Thanks.

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...

x axis and y axis labels in pheatmap in R

I really like how the pheatmap package creates very nice looking heatmaps in R. However, I am trying to add x and y axis labels to the output (if one were just in plot(), one would use: xlab = 'stuff'). A simple example is as follows.
require(pheatmap)
## Generate some data
d <- matrix(rnorm(25), 5, 5)
colnames(d) = paste("bip", 1:5, sep = "")
rownames(d) = paste("blob", 1:5, sep = "")
## Create the heatmap:
pheatmap(d)
The above yields the following heatmap:
I cannot for the life of me figure out how to add an 'xlab' or 'ylab' to this plot. Thoughts?
The main issue here is that pheatmap, which uses grid package, creates a new grid page each time it is called. The solution I've found is:
library(pheatmap)
library(grid)
## Generate some data
d <- matrix(rnorm(25), 5, 5)
colnames(d) = paste("bip", 1:5, sep = "")
rownames(d) = paste("blob", 1:5, sep = "")
## Create the heatmap:
setHook("grid.newpage", function() pushViewport(viewport(x=1,y=1,width=0.9, height=0.9, name="vp", just=c("right","top"))), action="prepend")
pheatmap(d)
setHook("grid.newpage", NULL, "replace")
grid.text("xlabel example", y=-0.07, gp=gpar(fontsize=16))
grid.text("ylabel example", x=-0.07, rot=90, gp=gpar(fontsize=16))

Resources