Place nodes explicitly with visNetwork (or an Alternative) - r

How can I explicitly place nodes on a visNetwork graph?
Or: How can I recreate that graphic in R using visNetwork or an alternative?
Background: The ultimate goal is to represent Causal Loop Diagrams coming from Vensim files. Placing the nodes explicitly is just the first (crucial) step, because in Causal Loop Diagrams the visual mapping of nodes is part of the information (unlike in general graph theory). So if anybody has advice on the bigger picture aka. 'Bringing Causal Loop Diagram Modeling to R', I'll be more than happy.
What I tried:
library("visNetwork")
nodes <- data.frame(id = 1:3, label = c("one", "two", "three"))
edges <- data.frame(from = c(1,1,2), to = c(2,3,1))
visNetwork(nodes, edges, width = "100%", title = nodes$labels, stringsAsFactors = FALSE) %>% visEdges(arrows = "to")
which plots something like (exact layout will change, because of random seed):
With the Q&A from here I tried to place nodes manually by setting x and y values.
library("visNetwork")
nodes <- data.frame(id = 1:3, label = c("one", "two", "three"), x = c(0,1,2), y = c(0,1,2))
edges <- data.frame(from = c(1,1,2), to = c(2,3,1))
visNetwork(nodes, edges, width = "100%", title = nodes$labels, stringsAsFactors = FALSE) %>% visEdges(arrows = "to")
which plots:
..and I really don't understand what's the correspondance between x, y and the placing on the screen..
Also I could not find anything in the docs for visLayout.

It somehow turns out, that the x and y args are not working. Here a solution:
library("visNetwork")
nodes <- data.frame(id = 1:3, label = c("one", "two", "three"))
edges <- data.frame(from = c(1,1,2), to = c(2,3,1))
coords <- as.matrix(data.frame(x = c(0,1,2),
y = c(0,1,2),
stringsAsFactors = FALSE))
visNetwork(nodes, edges, width = "100%", title = nodes$labels) %>%
visNodes() %>%
visOptions(highlightNearest = TRUE) %>%
visInteraction(navigationButtons = TRUE,
dragNodes = TRUE, dragView = TRUE,
zoomView = FALSE) %>%
visEdges(arrows = 'to') %>%
visIgraphLayout(layout = "layout.norm", layoutMatrix = coords)
For history see also here.
Perhaps these links might be helpful for what you want to achive: causaleffect and plot.CLD

Using ggraph instead of visNetwork simplifies things.
library(ggraph)
library(igraph)
g <- make_graph(edges = c(1,2,2,1,1,3))
V(g)$name <- c('one', 'two', 'three')
ggraph(g, layout = 'manual', node.positions = data.frame(x = c(1,1,2), y = c(2,1,2.1))) +
geom_edge_arc(aes(start_cap = label_rect(node1.name),
end_cap = label_rect(node2.name)),
angle_calc = 'along',
label_dodge = unit(2.5, 'mm'),
arrow = arrow(length = unit(4, 'mm'))) +
geom_node_text(aes(label = name, x = x, y = y))
This plots
which is (apart from gridlines and colours) what I was searching for.

Related

Change the layout of ring visNetwork graph

I create the ring graph below with visNetwork. I would like to know if I can change its default layout to a series of horizontal nodes, with a large curvy arrow going from the last one back to the front, like this below. The first will be the 1 and the last the 4:
library(visNetwork)
nodes <- data.frame(id = 1:4,label=1:4)
edges <- data.frame(from = c(1,2,3,4), to = c(2,3,4,1))
edges$length<-c(90,90,90,750)
edges$smooth<-c(F,F,F,T)
edges$label<-c("","","","")
coords <- matrix(ncol=2, byrow=T, data=c(
-9,0,
-2,0,
7,0,
14,0.5
))
visNetwork(nodes, edges, width = "100%") %>%
visIgraphLayout(layout = "layout.norm", layoutMatrix = coords) %>%
visNodes(shape = "square",
color = list(background = "lightblue",
border = "darkblue",
highlight = "yellow"),
shadow = list(enabled = TRUE, size = 10)) %>%
visLayout(randomSeed = 12) # to have always the same network

Show enlarged picture when mouse hovers above data point R

I am trying to plot a scatter plot that when the mouse hovers over one of the points, an image, corresponding to a URL in the data appears.
Is it possible in R? it seems it is possible in python...
thanks,
Here is a solution using the Highchater package:
library(highcharter)
df <- data.frame(x = c(1, 2, 3, 4),
y = rep(0, 4),
package = c("dplyr", "shiny", "purrr", "stringr"),
urlimage = c("https://github.com/rstudio/hex-stickers/raw/master/PNG/dplyr.png",
"https://github.com/rstudio/hex-stickers/raw/master/PNG/shiny.png",
"https://github.com/rstudio/hex-stickers/raw/master/PNG/purrr.png",
"https://github.com/rstudio/hex-stickers/raw/master/PNG/stringr.png"))
hover_info <- tags$tr(
tags$th("Package"),
tags$td(paste0("{point.package}")),
tags$img(src = "{point.urlimage}", width = "125px", height = "125px")) %>%
as.character()
highchart() %>%
hc_add_series(data = df,
mapping = hcaes(x = x, y = y),
type = "scatter",
marker = list(radius = 5, symbol = "circle")) %>%
hc_tooltip(
useHTML = TRUE,
headerFormat = "<table>",
pointFormat = hover_info,
footerFormat = "</table>"
)
Output:

display information in network edges using visNetwork

I am starting to learn about interactive graphs in R, and I found the library visNetwork very helpful.
However, I don't find on the vignettes how to display a pop-up with more information than the value and title when the mouse is moved over the edge.
Using one of the examples in the documentation
# data used in visNetwork vignette
nb <- 10
nodes <- data.frame(id = 1:nb, label = paste("Label", 1:nb),
group = sample(LETTERS[1:3], nb, replace = TRUE), value = 1:nb,
title = paste0("<p>", 1:nb,"<br>Tooltip !</p>"), stringsAsFactors = FALSE)
edges <- data.frame(from = c(8,2,7,6,1,8,9,4,6,2),
to = c(3,7,2,7,9,1,5,3,2,9),
value = rnorm(nb, 10), label = paste("Edge", 1:nb),
title = paste0("<p>", 1:nb,"<br>Edge Tooltip !</p>"))
visNetwork(nodes, edges, height = "500px", width = "100%")
How could I add more information to the pop-up, such as, different parameters related to the edge (width, frequency, ..)?
you have to paste all the information into the title column.
# data used in visNetwork vignette
nb <- 10
nodes <- data.frame(id = 1:nb, label = paste("Label", 1:nb),
group = sample(LETTERS[1:3], nb, replace = TRUE), value = 1:nb,
title = paste0("<p>", 1:nb,"<br>Tooltip !</p>"), stringsAsFactors = FALSE)
edges <- data.frame(from = c(8,2,7,6,1,8,9,4,6,2),
to = c(3,7,2,7,9,1,5,3,2,9),
value = rnorm(nb, 10), label = paste("Edge", 1:nb))
edges$title <- paste0(edges$label, "<br> value : ", round(edges$value, 2))
visNetwork(nodes, edges, height = "500px", width = "100%")

How to add text to a graph in diagrammeR

I want to create a nice graph that illustrates some of my data.
I have created the graph but I would like to add some calculated text to the node. How do I do this.
This is my graph but how/ where do I add a field I have calculated in R?:
library(magrittr)
library(DiagrammeR)
# Create a simple NDF
nodes <-
create_nodes(nodes = c("Index", "Surveillance", "Intervention", "Lost to Follow-up"))
# Create a simple EDF
edges <-
create_edges(from = c("Index", "Surveillance", "Index", "Surveillance","Intervention","Surveillance","Intervention"),
to = c("Surveillance", "Intervention", "Lost to Follow-up", "Lost to Follow-up","Intervention","Surveillance","Lost to Follow-up"),
)
graph <-
create_graph(
nodes_df = nodes,
edges_df = edges,
graph_attrs = "layout = twopi",
node_attrs = "fontname = Helvetica",
edge_attrs = "color = gray20"
)
# View the graph
render_graph(graph,output = "visNetwork")
require(visNetwork, quietly = TRUE)
nb = "Information here"
nodes <- data.frame(id = 1:5, group = c(rep("A", 2), rep("B", 3)),
title = paste("<p>", 1:5,"<br>",nb, sep = ""), stringsAsFactors = FALSE)
edges <- data.frame(from = c(2,5,3,3), to = c(1,2,4,2))
### USE
visNetwork(nodes, edges, width = "100%") %>% visOptions(highlightNearest = list(enabled =TRUE,algorithm="hierarchical"))
You will see your info when you pass the mouse on your node.

rCharts: Change the individual point colors of a time series plot (Highcharts)

I am trying to create a time-series plot using the plotting interface of rCharts to the Highcharts library.
I am trying to figure out how I can set the color of an individual point depending on its y-value. I found a way to have different colors for the line and the points, but only as a group, not for the data points individually.
Here's the test code:
library(rCharts)
library(rjson)
TransformDate <- function(x){
as.numeric(as.POSIXct(x, origin="1970-01-01")) * 1000
}
x <- TransformDate(c('2013-01-01 11:05:35', '2013-03-03 04:50:35', '2013-05-05 21:09:37', '2013-07-07 12:49:05'))
y <- c(1,56,123,1000)
w<-TransformDate(c('2013-01-10 11:05:35', '2013-03-13 04:50:35', '2013-05-15 21:09:37', '2013-07-17 12:49:05'))
z<-c(10, 100, 70, 500)
df1 <- data.frame(x = x,y = y)
df2 <- data.frame(x = w, y = z)
combo <- rCharts:::Highcharts$new()
combo$series(list(list(data = rCharts::toJSONArray2(df1, json = F, names = F), name = "Temp1", marker = list(fillColor = c('#999'), lineWidth=6, lineColor=c('#999'))),
list(data = rCharts::toJSONArray2(df2, json = F, names = F), name = "Temp2")))
combo$xAxis(type='datetime')
combo$chart(type = "scatter")
combo$chart(zoomType="x")
combo
I believe that this can be done in Polycharts but the reason why I am using highcharts is that it plots time-series data nicely and it has also cool zoom-in functionality.
Thanks in advance for your help & suggestions.
Jan
Here's one way to control color/size for lines/markers separately:
h <- rCharts:::Highcharts$new()
h$series(list(
list(data = rCharts::toJSONArray2(df1, json = FALSE, names = FALSE),
name = "Big Reds",
color = '#FF0000',
lineWidth = 4,
marker = list(
fillColor = '#FFA500',
radius = 10)
),
list(data = rCharts::toJSONArray2(df2, json = FALSE, names = FALSE),
name = "Small Blues",
color = '#0000FF',
lineWidth = 2,
marker = list(
fillColor = '#ADD8E6',
radius = 6)
)))
h$xAxis(type = 'datetime')
h$chart(type = "scatter")
h$chart(zoomType = "x")
h

Resources