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%")
Related
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
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.
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.
I'm trying to add horizontal and vertical lines in a highchart (rcharts) in a revealjs presentation.
I tried to modify the code of this post in this way:
require(xlsx)
library(rCharts)
Perhplot.df <-read.xlsx("C:\\RDirectory\\AREALAVORO\\JOB\\RISULTATI2.xlsx", sheetName="completo2")
lDf <- split(Perhplot.df, Perhplot.df$variable)
h16 <- hPlot(protection ~ days, data = lDf$Exposure,
type = "bubble",
group = "label",
title = "By Days of Exposure",
subtitle = "Move the mouse pointer on the bubbles to view the data",
size = "cluster_size",
group = "label")
h16$set(width = 1000, height = 600)
ord <- c("Less than 1 week"=0,
"1-2 weeks"=1,
"3-4 weeks"=2,
"More than 4 weeks"=3,
"Mean"=4
)
h16$params$series <- lapply(h16$params$series, function(d){
temp = ord[d$name]
names(temp) = NULL
d$legendIndex = temp
return(d)
})
h16$yAxis(min = 35, max = 70, title = list(text = "Level of Protection"))
h16$xAxis(min = 0, max = 45, title = list(text = "Days of Exposure"))
dfy<-data.frame(y=c(35,58,70), x=c(18.8,18.8,18.8))
h16$layer(y~x,data=dfy,type="line",color=list(const = 'darkblue'))
h16$show('inline', include_assets = TRUE)
the bubble plot is ok but then I try to add the vertical line I have this error:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘layer’ is not a valid field or method name for reference class “Highcharts”
So the solution works for Dimple Charts but not for Highcharts...
As same as rcharts highcharts plotLines.
You need to use plotLines argument:
library("rCharts")
# Some data
x <- abs(rnorm(10))
# A graph with column
h <- Highcharts$new()
h$chart(type = "column")
h$series(data = x)
h$xAxis(categories = letters[1:10])
# the horizontal line
h$yAxis(title = list(text = "rnorm()"),
plotLines = list(list(
value = mean(x),
color = '#ff0000',
width = 3,
zIndex = 4,
label = list(text = "mean",
style = list( color = '#ff0000', fontWeight = 'bold' )
))))
h
Yo add vertical you change yAxis by xAxis.
Or if you use highcharter (It's a new wrapper of highcharts for R):
h2 <- highchart() %>%
hc_chart(type = "column") %>%
hc_add_serie(data = x) %>%
hc_xAxis(categories = letters[1:10]) %>%
hc_yAxis(title = list(text = "rnorm()"),
plotLines = list(list(
value = mean(x),
color = '#ff0000',
width = 3,
zIndex = 4,
label = list(text = "mean",
style = list( color = '#ff0000', fontWeight = 'bold' )
))))
h2
Source: http://jkunst.com/highcharter/highcharts-api.html#hc_xaxis-and-hc_yaxis
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