DiagrammeR grViz Flow Chart Top Alignment - r

I am using DiagrammeR/grViz in R Studio to make a flow chart from left to right. I have included the R code below. This works fine but I would like to align Block 1 to Block 6 with the top so that they are flush at the top.
Any ideas or links to help would be appreciated.
library(DiagrammeR)
grViz("
digraph boxes_and_circles {
graph[rankdir = LR]
graph [overlap = false, fontsize = 10]
node[shape = rectangle, style = filled, style = rounded]
subgraph cluster_0 {
label = 'BLOCK 1'
node [shape = box,
fixedsize = true,
height = 0.7,
width = 1.2,
]
CS1[label = '1A'];
CS2[label = '1B'];
CS3[label = '1C'];
}
subgraph cluster_1 {
label = 'BLOCK 2'
node [shape = circle,
fixedsize = true,
width = 1.2,
] // sets as circles
CR1[label = '2A'];
CR2[label = '2B'];
}
subgraph cluster_3 {
label = 'BLOCK 4'
node [shape = box,
fixedsize = true,
height = 0.7,
width = 1.2,
]
RM1; RM2;
RM1[label = '4A'];
RM2[label = '4B'];
}
subgraph cluster_5 {
label = 'BLOCK 6'
node [shape = box,
fixedsize = true,
height = 0.7,
width = 1.2,
]
R1[label = '6A'];
R2[label = '6B'];
R3[label = '6C'];
}
CS1->CR1->RM1->R1
CS1-> R1
CS2->CR2->RM2->R1
CS1->CR2
CS3-> R3
}
")

Related

Creating a single graph with two different layouts (e.g. circle outside and unordered inside)

I'm attempting to produce a graph using graphviz that produces a visualisation like this fantastic one(!; https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwjcgcDdvcHjAhXG854KHaMHB2EQjRx6BAgBEAU&url=https%3A%2F%2Fwww.kathartman.com%2Fvisualizing-detroits-civic-tech-ecosystem&psig=AOvVaw1P66IWi0HPrMZQMmEP_MuS&ust=1563642864370970). My current strategy is to produce an outer circle of nodes (achieved), and now want to add the nodes to the centre of the visualisation (unsuccessful thus far!).
I've explored subgraph clusters, which seems the cleanest solution, but haven't found a way to change the layout from the circo layout. I've also explored merging two different graphs but haven't discovered a way to do this as yet. I've also tried other r packages (E.g. visnetwork, networkd3, circlize) with diagrammeR/graphviz seeming the most obvious solution again...
grViz("graph {
graph [bgcolor = 'white',layout = 'circo',
outputorder = 'edgesfirst']
subgraph cluster0 {
node [shape = 'rectangle',fontsize = 22,fontname = 'Helvetica',
fontcolor = 'black', width = 3, height = 1.25, style = 'dotted',
penwidth = 5]
'1'
'2'
'3'
node [shape = 'plaintext',fontsize = 16,fontname = 'Helvetica',
width = 1, height = 1]
'18'
'5'
node [shape = 'plaintext',fontsize = 16, fontname = 'Helvetica',
fontcolor = 'black']
'4'
'9'
'6'
node [fillcolor = OldLace,
fontsize = 11,fontsize = 14]
'7'
'8'
'21'
'22'
'23'
'10'
'20'
edge [weight = 0.5,penwidth = 4, fontname = 'Helvetica',
fontsize = '12', len = '1.5', color = 'gray80',
arrowsize = '0.5']
'1'--'18'
'18'--'4'
'4'--'7'
'7'--'8'
'8'--'2'
'2'--'5'
'5'--'9'
'9'--'21'
'21'--'22'
'22'--'23'
'23'--'3'
'3'--'6'
'6'--'10'
'10'--'20'
'20'--'1'}
subgraph cluster1 {
# Projects
node [fontname = 'Helvetica',
fontsize = '7', shape = 'circle',
fixedsize = 'true', width = '0.5',
style = 'filled', fillcolor = 'aliceblue',
fontcolor = 'gray50']
'11'
'12'
'13'
'14'
'15'
'16'
'17'
'19'
edge [weight = 0.25,fontname = 'Helvetica',
fontsize = '12', len = '1.5', color = 'gray80',
arrowsize = '0.5']
'6'--{'10' '17' '19'}
'7'--{'11' '19'}
'8'--{'12' '13' '14' '15'}
'9'--{'12' '13' '14' '16'}
}
}")
The goal is that the second subcluster (or 2nd graph) forms a network within the first subcluster, while still displaying the edges between. Currently, the 2nd subcluster populates the outside of the circle and remains in a circo layout

How to remove axes and numbers from Plotly

I have the following picture, and I want to remove everything besides the dots and the triangle, which means the numbers on the horizontal and vertical axes and the small vertical lines, how can I do it?
Here is the picture:
And here is my code:
x0 = np.average(triangleEdges,axis=0,weights=np.array([0.2,0.1,0.7]))[0]
y0 = np.average(triangleEdges,axis=0,weights=np.array([0.2,0.1,0.7]))[1]
x1 = np.average(triangleEdges,axis=0,weights=np.array([0.5,0.1,0.7]))[0]
y1 = np.average(triangleEdges,axis=0,weights=np.array([0.5,0.1,0.7]))[1]
trace0 = go.Scatter(
x=[x0],
y=[y0],
marker = dict(
size = 15,
color = 'rgba(25, 181, 254, 1)',
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
trace1 = go.Scatter(
x=[x1],
y=[y1],
marker = dict(
size = 15,
color = 'rgba(152, 0, 0, .8)',
line = dict(
width = 1,
color = 'rgb(0, 0, 0)'
)
)
)
data = [trace0,trace1]
layout = {
'xaxis': {
'range': [0.2, 1],
'zeroline': False,
},
'yaxis': {
'range': [0, 1],
'showgrid': False,
},
'shapes': [
# filled Triangle
{
'type': 'path',
'path': ' M 0.2 0 L 1 0 L 0.6 1 Z',
'fillcolor': 'rgba(44, 160, 101, 0.5)',
'line': {
'color': 'rgb(44, 160, 101)',
},
},
]
}
fig = {
'data': data,
'layout': layout,
}
py.iplot(fig, filename='shapes-path')
To turn off the axes:
'xaxis': {
'range': [0.2, 1],
'showgrid': False, # thin lines in the background
'zeroline': False, # thick line at x=0
'visible': False, # numbers below
}, # the same for yaxis
also if you want to remove the legend:
layout = {
'showlegend': False,
...
To turn off axes in newer versions:
#legend
fig.update_layout(showlegend=False)
#x axis
fig.update_xaxes(visible=False)
#y axis
fig.update_yaxes(visible=False)

Meta-analysis flowchart

Is it possible to reproduce a meta-analysis type of flowchart as the one in the picture below using any R tool?
My attempt was using mermaid:
diagram = "
graph LR
subgraph Screening
b1-->b2
end
subgraph Eligibility
c1-->c2
end
subgraph Included
d1-->d2
end
subgraph Identification
a1-->a2
end
"
mermaid(diagram)
Which generated:
But I cannot find a way of connect the nodes accross the subgraphs.
Is there another tool better fitting to this kind of job? I am thinking on any package that I could use from within my Rmarkdown document.
I have found the DiagrammeR package easiest to do this. The general idea would be something like:
library(glue)
library(DiagrammeR)
excluded <- glue('Full text articles excluded
n = 1000
Reasons for exclusion
Reason 1
Reason 2')
grViz("
digraph cohort_flow_chart
{
node [fontname = Helvetica, fontsize = 12, shape = box, width = 4]
a[label = 'Records identified in original search']
b[label = 'Records identified with update']
c[label = 'Records after duplicates removed']
d[label = 'Records screened']
e[label = 'Records excluded']
f[label = 'Full text articles assessed']
g[label = 'Studies included']
h[label = '##1']
{ rank = same; a b}
{ rank = same; d, e}
{ rank = same; f, h}
a -> c;
b -> c;
c -> d;
d -> e [ minlen = 3 ];
d -> f;
f -> h [ minlen = 3 ];
f -> g;
}
[1]: excluded
")
Will look like:
Image with labels and empty nodes
grViz("
digraph cohort_flow_chart
{
node [fontname = Helvetica, fontsize = 12, shape = box, width = 4]
i[label = 'Identification', fillcolor = LightBlue,
style = filled, width = 2]
j[label = 'Screening',fillcolor = LightBlue, style = filled, width = 2]
k[label = 'Eligibility', fillcolor = LightBlue, style = filled,
width = 2]
l[label = 'Included', fillcolor = LightBlue, style = filled, width = 2]
a[label = 'Records identified in original search']
b[label = 'Records identified with update']
c[label = 'Records after duplicates removed']
d[label = 'Records screened']
e[label = 'Records excluded']
f[label = 'Full text articles assessed']
g[label = 'Studies included']
h[label = '##1']
blank_1[label = '', width = 0.01, height = 0.01]
blank_2[label = '', width = 0.01, height = 0.01]
blank_4[label = '', width = 4, color = White]
{ rank = same; a b i}
{ rank = same; blank_4 c j}
{ rank = same; f k}
{ rank = same; g l}
{ rank = same; blank_1 e}
{ rank = same; blank_2 h}
a -> c;
b -> c;
b -> blank_4 [ dir = none, color = White];
c -> d;
d -> blank_1 [ dir = none ];
blank_1 -> e [ minlen = 3 ];
blank_1 -> f;
f -> blank_2 [ dir = none ];
blank_2 -> h [ minlen = 3 ];
blank_2 -> g;
}
[1]: excluded
")

How to increase the gap between the nodes in GraphViz without affecting the font size of label?

I am trying to create a flowchart with 24 primary nodes and some other secondary nodes using GraphViz i.e. grViz().
I wish to increase the gap between the nodes to have a clear view. But it is reducing the font size of the labels in each node, though I have kept the fontsize = 200 for the label of each node. This is what I have done.
library( DiagrammeR )
DiagrammeR(diagram = "", type = "grViz")
#.....Using GraphViz.....
grViz("
digraph dot {
graph[layout = dot,
rankdir = LR]
#....adding node statement for 's_o' key....
node[shape = circle, width = 20, style = filled, color = Black, fontname = Arial, fontsize = 200, penwidth = 20]
node[fillcolor = red]
's_o'
#....adding node statement for primary branching....
#....defining the primary keys.....
node[shape = box, width = 30, style = filled, color = Black, fontname = Arial, fontsize = 200, penwidth = 20]
node[fillcolor = green]
's_j'; 'f_s'; 'b_v'; 'o_c'; 'm_h'; 'a_v', 'o_o', 'm_u_ \nw_s'; 'p_u_ \n24_h'; 'b_d_ \nm'; 'm_u_ \ne_p_ \ns_p'; 's_s_p'; 's_s_p'; 'c_im_ \ns_p'; 'u_s_p'; 'v_im_ \ns_p'; 'd_f_ \ns_p'; 'm_b_f_ \n_p'; 'p_s_p'; 's_w_f_ \n_p'; 'ir_r_t_ \n10'; 'a_t_ \n10'; 'o_h_ \n24'
#.....adding edge statement for primary statement....
#.....mapping the main node with the primary nodes....
edge[arrowhead = normal, arrowsize = 10,minlen = 6, style = bold, color = Black, headclip = TRUE, tailclip = TRUE]
s_o -> {'s_j'; 'f_s'; 'b_v'; 'o_c'; 'm_h'; 'a_v', 'o_o', 'm_u_ \nw_s'; 'p_u_ \n24_h'; 'b_d_ \nm'; 'm_u_ \ne_p_ \ns_p'; 's_s_p'; 'c_im_ \ns_p'; 'u_s_p'; 'v_im_ \ns_p'; 'd_f_ \ns_p'; 'm_b_f_ \n_p'; 'p_s_p'; 's_w_f_ \n_p'; 'ir_r_t_ \n10'; 'a_t_ \n10'; 'o_h_ \n24'}
#....adding node statement for secondary branching....
#.....Defining the secondary keys for 's_j' key...
node[shape = box, height = 5, width = 12, style = filled, color = Black, fontname = Arial, fontsize = 150, penwidth = 20]
node[fillcolor = orange]
'1 : \n j'; '2 : \n j'; '...' ;
#.....From 'f_s' key...
node[shape = box, height = 5, width = 30, style = filled, color = Black, fontname = Arial, fontsize = 150, penwidth = 20]
node[fillcolor = orange]
't1 : \n 0 or c_t_v'; 'l_k_s_t'; 'h_ts';
#.....From 'b_v' key...
node[shape = box, height = 5, width = 30, style = filled, color = Black, fontname = Arial, fontsize = 150, penwidth = 20]
node[fillcolor = orange]
't1 : \n 0 or c_t_v'; 'l_k_s_t'; 'h_ts';
'b_ts'
#.....adding edge statement for secondary statement....
#.....From 's_j' key....
edge[arrowhead = normal, arrowsize = 10,minlen = 6, style = bold, color = Black, headclip = TRUE, tailclip = TRUE]
s_j -> {'1 : \n j'; '2 : \n j'; '...'}
#.....From 'f_s' key....
edge[arrowhead = normal, arrowsize = 10,minlen = 6, style = bold, color = Black, headclip = TRUE, tailclip = TRUE]
f_s -> {'t1 : \n 0 or c_t_v'; 'l_k_s_t';
'h_ts'}
#.....From 'b_v' key....
edge[arrowhead = normal, arrowsize = 10,minlen = 6, style = bold, color = Black,
headclip = TRUE, tailclip = TRUE]
b_v -> {'t1 : \n 0 or c_t_v'; 'l_k_s_t';
'h_ts'; 'b_ts'}
#....adding a graph statement.....
graph[nodesep = 2]
}
")
Output :

Can't Adjust Chart Margin

I created a Chart but it has a left margin between the Y.Axis and the Chart's border.
How can I make it 0?
And is it possible to define a 5px uniform margin (left, top, right, bottom) between the chart and the border?
My Chart code is the following:
Chart chart = new Chart {
AntiAliasing = AntiAliasingStyles.All,
TextAntiAliasingQuality = TextAntiAliasingQuality.High,
BackColor = Color.FromArgb(250, 250, 250),
Height = size.Height,
Width = size.Width
};
chart.Legends.Clear();
ChartArea area = new ChartArea {
BackColor = Color.Transparent,
BorderColor = Color.FromArgb(240, 240, 240),
BorderWidth = 1,
BorderDashStyle = ChartDashStyle.Solid,
AxisX = new Axis {
Enabled = AxisEnabled.True,
IntervalAutoMode = IntervalAutoMode.VariableCount,
IsLabelAutoFit = true,
IsMarginVisible = true,
LabelStyle = new LabelStyle { ForeColor = Color.FromArgb(100, 100, 100), Font = new Font("Arial", 10, FontStyle.Regular) },
LineColor = Color.FromArgb(220, 220, 220),
MajorGrid = new Grid { LineColor = Color.FromArgb(240, 240, 240), LineDashStyle = ChartDashStyle.Solid },
MajorTickMark = new TickMark { LineColor = Color.FromArgb(220, 220, 220), Size = 4.0f },
},
AxisY = new Axis {
Enabled = AxisEnabled.True,
IntervalAutoMode = IntervalAutoMode.VariableCount,
IsLabelAutoFit = true,
IsMarginVisible = true,
LabelStyle = new LabelStyle { ForeColor = Color.FromArgb(100, 100, 100), Font = new Font("Arial", 10, FontStyle.Regular) },
LineColor = Color.Transparent,
MajorGrid = new Grid { LineColor = Color.FromArgb(240, 240, 240), LineDashStyle = ChartDashStyle.Solid },
MajorTickMark = new TickMark { LineColor = Color.FromArgb(240, 240, 240), Size = 2.0f }
},
Position = new ElementPosition { Height = 100, Width = 100, X = 0, Y = 0 }
};
chart.ChartAreas.Add(area);
area.AxisX.LabelStyle.Format = "H:mm";
area.AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Hours;
Series series = new Series {
CustomProperties = "PointWidth = 1",
IsXValueIndexed = true,
XValueType = (ChartValueType)Enum.Parse(typeof(ChartValueType), x.Data.GetType().GetGenericArguments()[0].Name)
};
series.BorderWidth = 2;
series.BorderColor = Color.FromArgb(84, 164, 232);
series.ChartType = SeriesChartType.Area;
series.Color = Color.FromArgb(222, 234, 244);
series.Points.DataBindXY(x.Data, s.Data);
chart.Series.Add(series);
Thank You,
Miguel
You can remove the axis margin with Axis.IsMarginVisible = false.
You can position your charting areas using ChartArea.Position or position the plotting area using ChartArea.InnerPlotPosition

Resources