How to increase distance between nodes in DiagrammeR R - r

I have a nice graph with DiagrammeR in R studio, but the nodes are too clustered togather. I have searched everywhere but I cannot find a way of increasing the distance between them. Can I be shown?
Here is my code:
library(magrittr)
library(DiagrammeR)
# Create a simple NDF
nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
label = TRUE,
fontsize=55,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(30.5, 2.6, 9.4, 2.7))
edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ),
to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
rel = c(99, 6.7, 99, 99, 27, 22),
arrowhead = rep("normal", 6),
color = c("green", "green", "red", "red", "red", "red"))
graph <-
create_graph(
nodes_df = nodes,
edges_df = edges,
graph_attrs <-
c("layout = dot","overlap = FALSE","outputorder = edgesfirst"),
node_attrs <-
c("shape = circle",
"fixedsize = TRUE",
"width = 100",
"penwidth = 1",
"color = DodgerBlue",
"style = filled",
"fillcolor = Aqua",
"alpha_fillcolor = 0.5",
"fontname = Helvetica",
"fontcolor = Black"),
edge_attrs = "color = gray20")
# View the graph
render_graph(graph,layout=constant,output="visNetwork")

You could just set the length for the arrows between different nodes:
edges <- create_edges(from = c("Index", "Surveillance","Surveillance","Intervention", "Surveillance", "Index" ),
to = c("Surveillance", "Intervention","Surveillance","Intervention", "Lost", "Lost"),
rel = c(99, 6.7, 99, 99, 27, 22),
arrowhead = rep("normal", 6),
color = c("green", "green", "red", "red", "red", "red"),
length = c(200,200,50,50,200,200))
Or you could define a precise spot for each node:
nodes <- create_nodes(nodes = c("Index", "Surveillance", "Intervention","Lost"),
label = TRUE,
fontsize = 55,
type = "lower",
style = "filled",
color = "aqua",
shape = c("circle", "circle",
"rectangle", "rectangle"),
data = c(30.5, 2.6, 9.4, 2.7),
x = c(-80,80,-80,80),
y = c(-80,80,80,-80))

Related

Remove double legend plotly in R

I don't know why I have a double legend.
How to remove the second legend where it is written Aa
Code:
city = c("paris", "lyon", "lyon", "marseille", "lille", "toulouse", "bordeaux")
start = c("2018-08-04", "2018-07-25", "2018-07-30", "2018-07-29", "2018-08-03", "2018-08-04", "2018-08-03")
max = c(36.4, 37.2, 38.4, 37.4, 36.7, 34.9, 34.8)
duration = c(4, 3, 8, 10, 5, 4, 3)
tab = data.frame(city, start, duration, max)
tab$duration = as.integer(tab$duration)
t <- list(
family = "sans serif",
size = 14,
color = toRGB("grey50"))
bubbleplot <- plot_ly(tab, x = ~start, y = ~max,
text = ~paste(duration, "jours"),
color = ~city, mode='markers+text'
)
bubbleplot = bubbleplot %>% add_markers(
marker = list(size = ~duration*4, opacity = 0.7,
sizemode = "diameter"))
bubbleplot <- bubbleplot %>% add_text(textfont = t, textposition = "inside")
bubbleplot
Use the following,
t <- list(
family = "sans serif",
size = 14,
color = toRGB("grey50"))
bubbleplot <- plot_ly(tab, x = ~start, y = ~max,
text = ~paste(duration, "jours"),
color = ~city, mode='markers+text'
)
bubbleplot = bubbleplot %>% add_markers(
marker = list(size = ~duration*4, opacity = 0.7,
sizemode = "diameter")) %>% add_text(x = ~start, y = ~max,
text = ~paste(duration, "jours"), textfont = t, textposition = "inside", data = tab, inherit = F)
bubbleplot

Position edge labels on igraph plot in R

I'm trying to use igraph to plot a causal diagram between a few variables. I below is my code and basically everything I want in the graph, except that I cannot get the other two edge labels to move up above the edges, like the one that connects "stability" to "status".
ego <- c("Stability (high)", "Stability (high)", "Stability (high)")
alter <- c("Status", "Depressive symptoms", "Anxiety Symptoms")
association <- c("-", "-", "-")
nodes <- c("Stability (high)", "Status", "Depressive symptoms", "Anxiety Symptoms")
x <- c(-5, 5, 5, 5)
y <- c(4, 8, 4, 0)
edges <- as.data.frame(cbind(ego, alter, association))
nodes <- cbind.data.frame(nodes, x, y)
nodes$x <- as.numeric(nodes$x)
nodes$y <- as.numeric(nodes$y)
study1 <- graph_from_data_frame(edges, nodes, directed = TRUE)
E(study1)$color <- "red"
plot(study1, layout=as.matrix(nodes[,c("x","y")]),
vertex.size = 75,
vertex.color = "gray",
vertex.label.color = "black",
vertex.label.family = "Arial",
vertex.label.cex = 0.7,
edge.arrow.size = 0.7,
edge.width = 3.5,
edge.color = E(study1)$color,
edge.label = E(study1)$association,
edge.label.y = 0.5,
edge.label.cex = 3,
edge.label.color = "black")
You have to specify for each label the y-coordinate, i.e. edge.label.y = c(0.6, 0.2, -0.5) . I changed your code a little bit so you can see which label is which, i.e. association <- c("A", "B", "C")
The complete code:
library(igraph)
ego <- c("Stability (high)", "Stability (high)", "Stability (high)")
alter <- c("Status", "Depressive symptoms", "Anxiety Symptoms")
association <- c("A", "B", "C")
nodes <- c("Stability (high)", "Status", "Depressive symptoms", "Anxiety Symptoms")
x <- c(-5, 5, 5, 5)
y <- c(4, 8, 4, 0)
edges <- as.data.frame(cbind(ego, alter, association))
nodes <- cbind.data.frame(nodes, x, y)
nodes$x <- as.numeric(nodes$x)
nodes$y <- as.numeric(nodes$y)
study1 <- graph_from_data_frame(edges, nodes, directed = TRUE)
E(study1)$color <- "red"
plot(study1, layout=as.matrix(nodes[,c("x","y")]),
vertex.size = 75,
vertex.color = "gray",
vertex.label.color = "black",
vertex.label.family = "Arial",
vertex.label.cex = 0.7,
edge.arrow.size = 0.7,
edge.width = 3.5,
edge.color = E(study1)$color,
edge.label = E(study1)$association,
edge.label.y = c(0.6, 0.2, -0.5), # specify the y-coordinate for each label
edge.label.cex = 3,
edge.label.color = "black")
This wil result in:

How to include hover in a plotly layout element

I am stuck with the following plotly example which I would like to exploit:
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~uempmed, name = "unemployment")
# add shapes to the layout
p <- layout(p, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y")))
How can I include hover information for those rectangles? Neither of the following works:
layout(p, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
# text - hoverinfo pair
text = "hello", hoverinfo = "text",
y0 = 4, y1 = 12.5, yref = "y"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
# hovertext
hovertext = "good bye",
y0 = 4, y1 = 12.5, yref = "y")))
Is there any way how I can plot rectangles in plotly and apply a hover to them. Unfortunately, at least as I am aware of, there is no type = "rect" or anything like that available in R.
You can do it by adding polygons instead of shapes. Look below;
library(plotly)
plot_ly() %>%
add_polygons(x=c(as.Date("1980-01-01"), as.Date("1980-01-01"),
as.Date("1985-01-01"), as.Date("1985-01-01")),
y=c(4, 12.5, 12.5, 4),
line=list(width=0),
fillcolor='rgba(255, 212, 96, 0.5)', inherit = FALSE,
name = 'Hello') %>%
add_polygons(x=c(as.Date("2000-01-01"), as.Date("2000-01-01"),
as.Date("2005-01-01"), as.Date("2005-01-01")),
y=c(4, 12.5, 12.5, 4),
line=list(width=0),
fillcolor='rgba(255, 212, 96, 0.5)', inherit = FALSE,
name = 'Bye') %>%
add_markers(data = economics, x = ~date, y = ~uempmed, name = "unemployment")

Annotation / Text for ticklabels in R plotly polar chart

Let's say, I have a simple polar chart:
R code:
library(plotly)
p <- plot_ly(
type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers'
) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE#,
#tickmode="array",
#tickvals = c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5),
#ticktext = c('A', "B", "C", "D", "E", "F", "G", "H")
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Chart plotted:
When I set showticklabels = FALSE, the angle tick labels disappears, and then I want to put A,B,C,D,E,F,G and H at angles c(22.5, 67.5, 112, 157.5, 202, 247.5, 292, 337.5).
I am not able to get the below expected plot using ticktexts and tickvals.
Can someone please help me with getting me to the solution, or if it is possible with add_text or add_annotation ?
Expected plot:
you could remove all grid lines altogether in the angularaxis with showgrid = FALSE, or you can have a line per 22.5 degree starting from 0 and then the ticktext would be something like this c('', 'A', '', 'B', '', 'C', .......) , or you could tediously add the lines you expect to have and then remove the grid lines like this:
p <- plot_ly() %>%
# data points
add_trace(type = 'scatterpolar',
r = c(0,1,2,4),
theta = c(0,45,90,120),
size = c(10, 20, 30, 40),
sizes = c(100, 300),
mode = 'markers') %>%
# straight line from 0 dg to 180 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 180, 360),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 45 dg to 225 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 45, 225),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 90 dg to 270 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 90, 270),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# straight line from 135 dg to 315 dg
add_trace(type = 'scatterpolar',
r = c(0,4.3,4.3),
theta = c(0, 135, 315),
mode = 'lines',
line = list(color = 'grey', width = 1)) %>%
# fill circle of radius 1
add_trace(type = 'scatterpolar',
mode = 'lines',
r = 1,
theta =seq(0, 360, 0.1),
line = list(color = 'grey'),
fill = 'toself',
fillcolor = 'grey',
opacity = 0.5) %>%
layout(
showlegend = FALSE,
polar = list(
angularaxis = list(
showticklabels = TRUE,
# remove grid lines and ticks
showgrid = FALSE,
ticks = '',
# if you want the axis to go the other way around
# direction = 'clockwise',
tickmode="array",
tickvals = seq(22.5, 337.5, 45),
ticktext = LETTERS[1:8]
),
radialaxis = list(
tickmode="array",
tickvals = c(0, 1, 2, 3, 4, 5, 6, 7),
ticktext = c('', "One", "Two", "Three", "Four", "Five", "Six", "Seven")
)
)
)
ggplotly(p)
Output chart:
I noticed that the expected output you have there, lists the letters the other way around as you have them in your code. If this is something you also want just change the order of the letters to match the angles like this c("A", "H", "G", "F", "E", "D", "C", "B") (reversed order starting from A)

How to change font size in Euler plot in R?

I am trying to create a Euler diagram in R using the eulerr package. I would like to reduce the font size of the quantities text on the plot.
I have tried using cex=0.5 (as per example below) and have also tried fontsize = and font = but none have reduced the font size. Am I placing cex=0.5 in the wrong position?
library(eulerr)
set1 <- euler(c("A&B" = 3103,
"A&C" = 1034,
"A&D" = 118,
"B&C" = 2690,
"B&D" = 1017,
"C&D" = 1383,
"A&B&C" = 394,
"A&B&D" = 73,
"A&C&D" = 45,
"B&C&D" = 153,
"A&B&C&D" = 32))
eulerr.plot <- plot(set1,
fills = list(fill = c("#009292", "#FFB6DB", "#B66DFF", "#6DB6FF"), alpha = 0.7),
labels = NULL, quantities = TRUE, legend = list(labels = c("A", "B", "C", "D")), cex = 0.5)
You can change the text size of the quantities by passing a list defining the size to that argument like so:
plot(set1, fills = list(fill = c("#009292", "#FFB6DB", "#B66DFF", "#6DB6FF"), alpha = 0.7),
labels = NULL, quantities = list(cex = .5), legend = list(labels = c("A", "B", "C", "D")))

Resources