Hi all,
I am trying to plot a graph on a Cartesian plane (Image 1 is the original graph). The graph has fixed number of nodes and the connections between nodes are constant. The graph is undirected.
I managed to create the graph using igraph and plot in using ggraph() (see Image 2). I have included the code I used to plot it with ggraph. However, with the latest upgrade the ggraph() is unable to draw the plot.
Can you please suggest me a way to achieve (plotting on a Cartesian plane) the same in igraph? I am using R 4.0.3.
library(igraph)
library(tidyverse)
library(ggraph)
#Setting Node coordinates
V <- read.table(text = "x y
2 1
4 2
4 4
2 5
6 4
3 7
8 6",
header = T) %>%
tibble::rownames_to_column("name")
#connection between nodes
E <- matrix(c(0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0), nrow = 7, byrow = T) %>%
data.frame() %>%
rename_all(list(function(x) 1:7)) %>%
tibble::rownames_to_column(var = "from") %>%
gather(to, val, 2:6) %>%
filter(val == 1) %>%
select(from, to)
#generate the igraph
g <- graph_from_data_frame(E, vertices = V, directed = F)
#plot the graph
ggraph(g) +
geom_edge_link(edge_width = 1.3) +
geom_node_label(aes(label = name),label.r = unit(0.75, "lines"),
label.size = 0.65, label.padding = unit(0.55,"lines"), show.legend = F) +
ggtitle("My plot") +
coord_flip() +
expand_limits(x = 0, y = 0) +
# Using scale_x_reverse and swapping the limits
scale_x_reverse(expand = c(0, 0), limits = c(9, 0), breaks = c(0:9), minor_breaks = NULL) +
# switching y position to "right" (pre-flip)
scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL, position = "right") +
theme_minimal()