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:
Related
I have a dataframe with 3 columns, example like this (purely hypothetical):
id <- c("Muller", "Muller", "Ter Stegen", "Musiala", "Musiala", "Musiala", "Pavard")
tid <- c("Davies", "De Ligt", "Muller", "Kimmich", "Pavard", "Lewandowski", "De Ligt")
Passes <- c(14, 5, 1, 10, 23, 4, 1)
Passes <- data.frame(id, tid, Passes)
dput(Passes)
And I have been wanting to plot this so that the vertices appear at specific coordinates in the output graph .
So far my codes are like this:
g <- graph.data.frame(Passes, directed = TRUE)
set_edge_attr(g, "weight", value= E(g)$Passes)
coords <- data.frame(id = c("Ter Stegen", "Musiala", "Davies", "Kimmich", 'De Ligt', "Lewandowski", "Muller", "Pavard"),
x= c(0.5, 1, 1, 1, 2, 3, 3, 3.5),
y= c(1, 1.8, 1.4, 1, 0.6, 1.8, 1.6, 1.2))
plot(g, vertex.size= 2, edge.arrow.size = 0.3, vertex.label.cex = 0.8,
edge.curved=.2, asp = 0, vertex.label.dist=0.7,
layout=coords, xlim = c(0, 4), ylim = c(0, 2))
But then I keep getting errors like 'Error in norm_coords(layout, -1, 1, -1, 1) : `layout' not a matrix''
Anyone know what is wrong with my code, or can propose a better method? Thank you! It's just my actual dataframe has 32 unique ids and together there are 252 rows, I want to find an efficient way to give each unique id a position.
Thanks,
Emmy
try
library(tidyverse)
new.coords <- coords %>% arrange(factor(id, levels = V(g))) %>% select(x,y) %>% as.matrix()
plot(g, vertex.size= 2, edge.arrow.size = 0.3, vertex.label.cex = 0.8,
edge.curved=.2, asp = 0, vertex.label.dist=0.7,
layout = new.coords)
I am working with the R programming language. I am trying to make a "parallel coordinates plot" using some fake data:
library(MASS)
a = rnorm(100, 10, 10)
b = rnorm(100, 10, 5)
c = rnorm(100, 5, 10)
d = matrix(a, b, c)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
However, a problem arises when I try to mix numeric and factor variables together:
group <- sample( LETTERS[1:4], 100, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
d = matrix(a,b, group)
parcoord(d[, c(3, 1, 2)], col = 1 + (0:149) %/% 50)
Error in x - min(x, na.rm = TRUE): non-numeric argument to binary operator
I am just curious. Can this problem be resolved? Or is it simply impossible to make such a plot using numeric and factor variables together?
I saw a previous stackoverflow post over here where a similar plot is made using numeric and factor variables: How to plot parallel coordinates with multiple categorical variables in R
However, I am using a computer with no USB port or internet access - I have a pre-installed version of R with limited libraries (I have plotly, ggplot2, dplyr, MASS ... I don't have ggally or tidyverse) and was looking for a way to do this only with the parcoord() function.
Does anyone have any ideas if this can be done?
Thanks
Thanks
One option is to label rows of the matrix using a factor and use that on the plot, e.g.
library(MASS)
set.seed(300)
par(xpd=TRUE)
par(mar=c(4, 4, 4, 6))
a = rnorm(12, 10, 10)
b = rnorm(12, 10, 5)
c = rnorm(12, 5, 10)
group <- sample(c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"),
12, replace=TRUE)
d = cbind(a, b, c)
rownames(d) <- group
parcoord(d[, c(3, 1, 2)], col = group)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 1, 0.1),
tick = TRUE, las = 1)
legend(3.05, 1, legend = c("A", "B", "C", "D"), lty = 1,
col = c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"))
EDIT
Thanks for the additional explanation. What you want does make sense, but unfortunately it doesn't look like it will work as I expected. I tried to make a plot using an ordered factor as the middle variable (per https://pasteboard.co/JKK4AUD.jpg) but got the same error ("non-numeric argument to binary operator").
One way I thought of doing it is to recode the factor as a number (e.g. "Var_1" -> 0.2, "Var_2" -> 0.4) as below:
library(MASS)
set.seed(123)
par(xpd=TRUE)
par(mar=c(4, 4, 4, 6))
a = rnorm(12, 10, 10)
b = c(rep("Var_1", 3),
rep("Var_2", 3),
rep("Var_3", 3),
rep("Var_4", 3))
c = rnorm(12, 5, 10)
group <- c(rep("#FF9289", 3),
rep("#FF8AFF", 3),
rep("#00DB98", 3),
rep("#00CBFF", 3))
d = data.frame("A" = a,
"Factor" = b,
"C" = c,
"Group" = group)
d$Factor <- sapply(d$Factor, switch,
"Var_1" = 0.8,
"Var_2" = 0.6,
"Var_3" = 0.4,
"Var_4" = 0.2)
parcoord(d[, c(1, 2, 3)], col = group)
title(main = "Plot", xlab = "Variable", ylab = "Values")
axis(side = 2, at = seq(0, 1, 0.1),
tick = TRUE, las = 1)
legend(3.05, 1, legend = c("A", "B", "C", "D"), lty = 1,
col = c("#FF9289", "#FF8AFF", "#00DB98", "#00CBFF"))
mtext(text = "Var 1", side = 1, adj = 0.6, padj = -30)
mtext(text = "Var 3", side = 1, adj = 0.6, padj = -12)
mtext(text = "Var 2", side = 1, adj = 0.6, padj = -21)
mtext(text = "Var 4", side = 1, adj = 0.6, padj = -3)
I am trying to duplicate a plot found here on pg. 4:
The reproducible code for it is:
require(devtools)
install_git("https://github.com/marchion/git.switchBox", subdir="switchBox")
require(switchBox)
require(gplots)
data(trainingData)
classifier <- SWAP.KTSP.Train(matTraining, trainingGroup)
kappa <- SWAP.KTSP.Statistics(matTraining, classifier)
mat <- t(1*kappa$comparisons)
rownames(mat) <- gsub(">", "\n more express than\n", rownames(mat))
heatmap.2(mat,
scale="none", Rowv=F, Colv=F, dendrogram="none",
trace="none", key=FALSE,
col=c("lightsteelblue2", "pink3"),
labCol=toupper(paste(trainingGroup, "Prognosis")),
sepwidth=c(0.075,0.075), sepcolor="black",
rowsep=1:ncol(kappa$comparisons),
colsep=1:nrow(kappa$comparisons),
lmat=rbind( c(0, 3), c(2, 1), c(0, 4) ), lhei=c(0.1, 5, 0.5), lwid=c(0.15, 5),
mar=c(7.5, 12), cexRow=0.85, cexCol=0.9)
If you notice in the plot above, the x-labels are slightly off-center to the left. Is there a command inside the heatmap.2 function that can shift each label to the right?
You have to specify argument adjCol (c(1, 0.5)) would give you wanted result (c(1, 0) would move it to the left and c(1, 1) would move it more to the right).
Code (using OPs provided packages and data):
heatmap.2(
mat,
adjCol = c(1, 0.5),
scale = "none", Rowv = FALSE, Colv = FALSE, dendrogram = "none",
trace = "none", key = FALSE,
col = c("lightsteelblue2", "pink3"),
labCol = toupper(paste(trainingGroup, "Prognosis")),
sepwidth = c(0.075,0.075), sepcolor = "black",
rowsep = 1:ncol(kappa$comparisons),
colsep = 1:nrow(kappa$comparisons),
lmat = rbind( c(0, 3), c(2, 1), c(0, 4) ),
lhei = c(0.1, 5, 0.5), lwid = c(0.15, 5),
mar = c(7.5, 12), cexRow = 0.85, cexCol = 0.9,
)
Result:
I am building a dendrogram for the first time and the rectangles around clusters are drawn on top of the labels. Do you know how modify the positioning of these labels in order to avoid this overlap?
Here you can find a working example of my code:
mydata <- c(9.45, 10.54, 10.36, 10.46, 10.78, 10.1, 11.13)
mydata.matrix <- matrix(mydata, nrow = 1, ncol = 7)
colnames(mydata.matrix) <- c("a", "b", "c", "d", "e", "f", "g")
rownames(mydata.matrix) <- c("X")
d <- dist(mydata.matrix["X", ], method = "euclidean")
fit <- hclust(d, method="ward.D")
nodePar <- list(lab.cex = 0.6, pch = c(NA, 19), cex = 0.7, col = "blue")
plot(as.dendrogram(fit), xlab = "", sub="", ylab = "Euclidean distance",
main = "Dendrogram", nodePar = nodePar)
rect.hclust(fit, k=2, border="red")
And here is the plot from the code above:
In particular I would like to have the red rectangles contain entirely the labels of the leaves of the dendrogram.
Thank you!
You should use the rect.dendrogram function from the dendextend package.
For example:
mydata <- c(9.45, 10.54, 10.36, 10.46, 10.78, 10.1, 11.13)
mydata.matrix <- matrix(mydata, nrow = 1, ncol = 7)
colnames(mydata.matrix) <- c("a", "b", "c", "d", "e", "f", "g")
rownames(mydata.matrix) <- c("X")
d <- dist(mydata.matrix["X", ], method = "euclidean")
fit <- hclust(d, method="ward.D")
nodePar <- list(lab.cex = 0.6, pch = c(NA, 19), cex = 0.7, col = "blue")
dend <- as.dendrogram(fit)
plot(dend, xlab = "", sub="", ylab = "Euclidean distance",
main = "Dendrogram", nodePar = nodePar)
library(dendextend)
rect.dendrogram(dend , k=2, border="red")
And you will get:
In general, for plotting dendrograms, you might find the following quick introduction to dendextend useful (or look at the more lengthy version).
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))