thomasp85 / ggraph

Grammar of Graph Graphics
https://ggraph.data-imaginist.com
Other
1.08k stars 116 forks source link

The direction of arc (up or down) can not be controled #378

Open yancychy opened 4 weeks ago

yancychy commented 4 weeks ago

Hi, I want plot all arcs above the x axis. But when I set the x-axis position for each node. Some arcs are down and some arcs are up. Is there any way to make all arcs above the same y value? Thank you.

Here is an normal condition. norm

The code of normal condition is:

library(tidyverse, warn.conflicts = FALSE)
library(tidygraph, warn.conflicts = FALSE)
library(igraph, warn.conflicts = FALSE)
library(ggraph, warn.conflicts = FALSE)

# make random sim data reproducible
set.seed(1)

# define nodes
nodeNum = 10
nodes <- data.frame(node_name = paste0("node", 1:nodeNum),
                    xPos = log2(seq(1, 100, nodeNum)+10)
                    )
rownames(nodes) = nodes$node_name

cn = cbind(nodes$node_name[5], nodes$node_name )

# define edges
edges <- cn %>%
  as_tibble(.name_repair = "universal") %>%
  rename(from = 1, to = 2) %>%
  mutate(edge_width = sample(x = 10:1000, size = nrow(.), replace = T))
edges$startX = nodes[edges$from,2]
edges$endX = nodes[edges$to,2]

# build network from nodes and edges
network <- tbl_graph(edges = edges, nodes = nodes, directed = FALSE)

# visualize network as arcplot
network %>%
  ggraph(layout = "linear") +
  geom_edge_arc(aes(color = edge_width >= 500,
                    width = abs(edge_width),
                    strength = sign(edge_width)),
                alpha = 0.65) +
  geom_node_label(aes(label = node_name), size = 3)

Here is an reverse condition. reverse

The code of reverse condition is:

yh = 2
pt1 = ggraph(network, layout = 'linear') +
  geom_edge_arc(aes(x=startX, xend=endX, y=yh, yend=yh, #width = weight,
                    col = edge_width >= 500,
                    circular = FALSE, strength= sign(edge_width)),
                alpha = 0.7, width=1.5,
                fold = TRUE) +
  geom_node_point(aes(x=xPos,  y=yh), size=3) +
  scale_shape_manual(values=c(17, 15, 16), name="")+
  geom_hline(yintercept = 0,  alpha=1) +
  labs( edge_color =  "Weight", size = "ss") +
  theme_classic() +
  scale_x_continuous(n.breaks=6, labels = scales::comma)

pt1