Options in buildEdge shinyCyJS - r

I'm making a graph in Shiny using shinyCyJS, I'm building the edges with buildElems that calls buildEdge:
edges = data.frame(
source = c("v1","v2","v3","v4","v4"),
target = c("v2","v3","v4","v2","v1"),targetArrowShape ='triangle',targetArrowColor = "#000000"
)
edges = buildElems(edges, type = 'Edge')
But the parameters aren't working, I want to:
Build a simple arrow from source to target.
Make the edge black.
Write a legend above the edge.
How can I do those? Thanks.

This works:
edges = data.frame(
source = c("v1","v1","v2","v3","v3"),
target = c("v2","v3","v4","v2","v4"),
label = c("7","3","2","2","8"),
lineColor = "#9b9b9b",
curveStyle='bezier',
targetArrowColor = 'black',
sourceArrowColor = 'black',
targetArrowShape ='triangle',
sourceArrowShape = 'none'
)

Related

How to use a newline in node label - forceNetwork R Shiny

I am trying to include the ability to have newlines in a node label, otherwise they extend out pretty far and just look messy in my R Shiny application. How can I do this? I have tried including the html break character and newline character but neither has worked. My network is created using the following code
forceNetwork(
Links = edges,
Nodes = nodes,
fontFamily = "Arial",
Nodesize = "size",
Source = "source_id",
Target = "target_id",
NodeID = "type_2",
Group = "type",
zoom = TRUE,
opacity = 1,
charge = -100,
linkColour = color_string_link,
opacityNoHover = ifelse(input$togglelabels == "labels",1,0),
colourScale = JS(color_string)
)

Is it possible to customize the size of rpivottable treemaps

This code works and the pivot table is drawn but the treemap is huge and I need it to render to about 50% of the current size, 500px would be fine for now. It seems to ignore the size settings. One pivot table overlaps the other when drawn.
output$pivot3 <- renderRpivotTable({
rpivotTable(data = dotsData,
rows="county",
cols="shortBreath",
width="500px",
height="500px",
aggregatorName = "Count",
aggregators = list(
Percentage = htmlwidgets::JS('$.pivotUtilities.aggregators["Count as Fraction of Columns"]'),
Count = htmlwidgets::JS('$.pivotUtilities.aggregators["Count"]')),
sorters="function(attr) {
var sortAs = $.pivotUtilities.sortAs;
if (attr == \"county\") {
return sortAs;
}
}")
})
I left a comment not that long ago of a method in which you could control the size by changing the JS. However, I have an R-based solution now.
I forked the Cran version of this package and modified it. My version of this package does allow you to control the size of the Treemap.
To install my modified packaged, use the following:
devtools::install_github("fraupflaume/rpivotTable")
You'll call the library the same way you did with the Cran version. To modify the treemap, you need to know that this is a D3 graph. All of the other graphs in this package are C3.
The following example reflects how you would indicate a specific size for either D3 or C3. (I've included several controllable options for C3 graphs, as well.)
library(rpivotTable)
rpivotTable(mtcars, rows = c("mpg", "am"), cols = "cyl",
width = "90%", height = "40%",
rendererOptions = list(
c3 = list(legend = list(show = FALSE),
data = list(labels = TRUE),
options = list(responsive = TRUE,
maintainAspectRatio = FALSE),
size = list(width = "600",
height = "500")),
d3 = list(size = list(width = "500", height = "500"))))
I expanded the viewer pane so that you could see it no longer fills the viewer.
This is the same view, but with the D3 controls removed.

R visNetwork: create new type of edges

I want to create a PAG(parental ancestral graph) with visNetwork for my shiny app.
In order to do that i have to create edges that have both circles and arrows.
According to the visNetwork package i can convert the arrows to circles like this
visNetwork(nodes, edges) %>%
visEdges(arrows = list(to = list(enabled = TRUE,
scaleFactor = 2, type = 'circle')))
But i want to have both an arrow and a circle, or two circles in one edge like in this picture
PAG
The arrows.from.type and arrows.to.type seem to be working but i now i have this problem.
I want to draw this graph according to an adjacency matrix
So i have this code
i = 1
j = 1
for(i in i:ncol(results))
{
j = i
for(j in j:nrow(results))
{
if(results[j,i]==1)
{
dashBoard = c(dashBoard,TRUE)
colorBoard = c(colorBoard, "green")
if(results[i,j]==1)
{
fromtest <- c(fromtest,Cnames[i])
totest <- c(totest,Rnames[j])
arrfrom <-c(arrfrom,"circle")
arrto<-c(arrto,"circle")
}
else if(results[i,j]==2)
{
fromtest<-c(fromtest,Cnames[i])
totest<-c(totest,Rnames[j])
arrfrom <-c(arrfrom,"circle")
arrto<-c(arrto,"arrow")
}}
That goes on for every possible combination except 1,1 and 1,2
In the end the edges are printed like that
edgesprint <-data.frame(from = fromtest,
to = totest,
arrows.from.type=arrfrom,
arrows.to.type=arrto,
dashes = dashBoard,
physics = FALSE,
smooth = FALSE,
width = 3,
shadow = TRUE,
color = list(color = colorBoard, highlight = "red", hover = "green"),
links = links)
This method works good but sometimes without changing any code i get this error
error in data.frame arguments imply differing number of rows
You can set individual arrow types in the edges data frame by adding columns arrows.to.type and arrows.from.type:
library(visNetwork)
library(magrittr)
nodes <- data.frame(id=c("a","b","c","d"), label=c("a","b","c","d"))
edges <- data.frame(
from = c("a","a","a"),
to = c("b","c","d"),
arrows.from.type = c(NA,"circle","circle"),
arrows.to.type = c("arrow","circle",NA)
)
visNetwork(nodes, edges)
Result:
This approach works for all other attributes you can set through visNodes and visEdges. See here for an example.

Gradient color in links of sankey chart using networkD3

I am using networkD3 to create sankey diagrams in R. I am using something like below to generate the diagrams:
sankeyNetwork(Links = data$links, Nodes = data$nodes,
Source = "source", Target = "target", Value = "value", NodeID = "name",
units = "Questions", fontSize = 12, nodeWidth = 10,
colourScale = "d3.scale.category10()",
LinkGroup = "type", #<--assign colors to links
width = 1000, height = 600)
I am using LinkGroup = "type" to color the links, where the type is basically the source node's name. This makes the link color same as source node color. This is working pretty well.
I was wondering if there is anyway a gradient of color can be created from sources to targets for the associated links?

Own colour range for Sankey Diagram with networkD3 package in R

I am trying to plot Sankey diagrams using sankeyNetwork() in networkD3 package.
sankeyNetwork(Links = Flow_data, Nodes = Nodes_data,
Source = "Source_ID", Target = "Target",
Value = "value", NodeID = "Nodes_name",
width = 1000, height=600, fontsize = 16, nodeWidth = 50,
colourScale = "d3.scale.category20c()")
The visualization works great but I would like to change the colour range to an individual range. Is there any chance to change the colours of the SankeyNetwork? I need a range of only e.g. 3 colours which I can set by myself (not the predefined colourScales of d3.scale).
You can config:
sankeyNetwork(Links = Flow_data, Nodes = Nodes_data,
Source = "Source_ID", Target = "Target",
Value = "value", NodeID = "Nodes_name",
width = 1000, height=600, fontsize = 16, nodeWidth = 50,
colourScale = "d3.scale.category20c()") <==== Categorical color
UPDATE
Newer version:
d3.scale.ordinal().range(["#7d3945","#e0677b", "#244457"])
now works if changed to:
d3.scaleOrdinal().range(["#7d3945","#e0677b", "#244457"])
Thanks #Peter Ellis
UPDATE
Is there any way to set transparency when using custom colours?
"#AARRGGBB" doesn't seem to work
You can make a selectAll("your_class").style("opacity",0.5), Take a look to this: stackoverflow.com/questions/6042550/… for style attribute options. And CSS3 has a fully standardized solution: "fill="rgba(124,240,10,0.5)"
For color references, look here: http://bl.ocks.org/aaizemberg/78bd3dade9593896a59d
and here: https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors

Resources