如何在GGR图形中翻转x轴和y轴

如何在GGR图形中翻转x轴和y轴,r,ggplot2,ggraph,R,Ggplot2,Ggraph,第一个图像是图形的手绘(使用MS word)图像。第二个图像是尝试使用ggraph生成相同的图形 下面是我用来在给定节点边缘连接时自动绘制图形的代码(代码采用自)。我想移动ggraph的x轴和y轴,如手绘图(图1)所示。从上到下颠倒x轴编号,并将y轴移动到顶部。我该怎么做呢 library(igraph) library(tidyverse) library(ggraph) V <- read.table(text = "x y 2 1

第一个图像是图形的手绘(使用MS word)图像。第二个图像是尝试使用ggraph生成相同的图形

下面是我用来在给定节点边缘连接时自动绘制图形的代码(代码采用自)。我想移动ggraph的x轴和y轴,如手绘图(图1)所示。从上到下颠倒x轴编号,并将y轴移动到顶部。我该怎么做呢

library(igraph)
library(tidyverse)
library(ggraph)

V <- read.table(text = "x        y
                2 1
                4 2
                4 4
                2 5
                6 4
                3 7
                8 6", 
    header = T) %>%
  rownames_to_column("name")

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)) %>% 
  rownames_to_column(var = "from") %>% 
  gather(to, val, 2:6) %>% 
  filter(val == 1) %>%
  select(from, to)

g <- graph_from_data_frame(E, vertices = V, directed = F)

png("C:\\Users\\Yasoda\\Downloads\\rplot.png", width = 450, height = 450)

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) +
  scale_x_continuous(expand = c(0, 0), limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) + 
  scale_y_continuous(expand = c(0, 0),limits = c(0, 9), breaks = c(0:9), minor_breaks = NULL) +
  theme_minimal() 


dev.off()
库(igraph)
图书馆(tidyverse)
图书馆(ggraph)
V%
行名称到列(“名称”)
E%
data.frame()%>%
重命名所有(列表(函数(x)1:7))%>%
行名称到列(var=“from”)%>%
聚集(到,val,2:6)%>%
筛选器(val==1)%>%
选择(从、到)
G

这个例子似乎需要一些修正:
g@JuliusVainora道歉。我现在已经修好了。这是将ID列添加到数据框V中。我删除了该部分。现在应该可以了。
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()