0

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

Emmy W
  • 21
  • 4
  • 1
    ypour code is not reproducible.. It throws errors (for eaxmple when craeteing `coords`, but also `vertex_colours` does not exits). Please try it yourself and also, please add the result of `dput(Passes)` – Wimpel Feb 13 '23 at 14:17
  • Hi, thank you for replying, I just didn't put down the section where I coded for the vertex_colours as I thought it wasn't relevant too the question. Also, can you please explain what dput(Passes) means? Do you mean the table I provided at the start? – Emmy W Feb 13 '23 at 14:20
  • [See here on how to make a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). `dput()` is a way of providing a dataset or object. – jrcalabrese Feb 13 '23 at 14:24
  • yes.. your sample data in its current form is not copy-pastable – Wimpel Feb 13 '23 at 14:25
  • also `"Ter Stegen"` from your coords, does not appear in the graph.. He is probably called `"Stegen"` there? Also in coords, x has length 8, but y has length 9. Please test your sample input before posting.. It saves people a lot of time, and will (probably) get you better answers. – Wimpel Feb 13 '23 at 14:29
  • Hi, Thank you for your advice, I made some updates, hopefully this is better than before. – Emmy W Feb 13 '23 at 14:32

1 Answers1

2

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)

enter image description here

Wimpel
  • 26,031
  • 1
  • 20
  • 37