0

I think I have lost my weekend dealing with this error for this I decided to post my code headache.

I have this matrix

library(tidyverse)
library(tidygraph)
library(ggraph)
library(scales)

mat <- matrix(c(10,10,0,2,5,0,1,
         10,9,1,2,6,0,1,
         10,9,1,1,5,1,1,
         10,10,1,2,6,1,1,
         10,10,2,6,7,1,1,
         10,9,7,6,7,3,2,
         10,10,2,6,7,1,1,
         10,7,2,5,7,1,1,
         10,10,2,5,7,1,1,
         10,9,2,5,7,1,1,
         10,10,2,6,7,1,1), nrow=11, ncol=7, byrow = TRUE)
colnames(mat) <- c(0,12,40,50,60,70,90)

rownames(mat) <- c(12,20,30,40,50,60,70,80,90,100,120)

and I plot it as bipartite network with ggraph and then I am taking a completely distorted/crazy x-axis.

(layout <- create_layout(mat, "bipartite"))

ggraph(layout) + 
  geom_edge_link0(aes(edge_width = weight),edge_colour = "black", alpha=0.9) +
  scale_edge_width(range = c(1, 10)) +
  geom_node_point(aes(shape = type, colour = type), size = 10) +
  scale_colour_manual(values = c("#02BEC4", "#45BF44")) +
  scale_shape_manual(values = c(15, 19)) +
scale_x_continuous(
    breaks = layout$x[!layout$type],
    labels = layout$name[!layout$type],
    position = "top",
    sec.axis = dup_axis(
      breaks = layout$x[layout$type],
      labels = layout$name[layout$type],
    )
  ) +
  theme(axis.text = element_text())

Why my x-axis can not start from 0?

enter image description here

I want to see in the x-axis at the bottom the following order, 0,12,40,50,60,70,90 and not 90,12,60,50,0,40,70 I would highly appreciate any help in the topic.

footnote: Many thanks to Martin Gal for his comment

LDT
  • 2,856
  • 2
  • 15
  • 32
  • 1
    I don't know how to solve your problem. The matrix' column names are on your x-axis. Inside the `layout`-object, those are saved as `characters` and I think that's the problem. You can change the order, for example with `layout$x <- c(layout$x[1:11], 0:6)`, but that's not a very fine solution. What exactly are you trying to plot, since I miss the "real" values inside your matrix in your actual plot – Martin Gal Jul 11 '21 at 16:19
  • Thank you Martin. I ll update the question based on your comment. I want to see in the x-axis at the bottom the following order, 0,12,40,50,60,70,90 and not 90,12,60,50,0,40,70. It does not matter really for me now what how the values are plotted. I mean I have check it they are plotted nicely but the order in the x-axis is messed up – LDT Jul 11 '21 at 16:20
  • Over all I try to plot how species in different time points interact. I have the blue species and the green and I see how much blue affects green at time 12 etc... Does it make sense – LDT Jul 11 '21 at 16:30
  • 2
    If you want the x-axis positions at `dimnames(mat)`, you can do `layout$x <- as.numeric(as.character(layout$name))`. – teunbrand Jul 11 '21 at 18:34
  • Thank you @teunbrand. You saved my weekend. I need to understand deeper hoe these network graphs work. Thank you for the help – LDT Jul 12 '21 at 07:04

0 Answers0