0

向外的位置几乎可以在我的网络中完成我想要的(左标签在左边,从右到右)。

但是我想稍微修改它以避免与节点重叠。我尝试在 gtable ( ggplot_build, ggplot_gtable) 中搜索要修改的内容但没有成功。在 gtable 中,标签的位置显示为outwards,而不是我可以修改的数字。

即使我更改outwards为其他选项,我看到的只是一个象征性的理由价值,而不是真正的头寸价值。

最小的例子:

#
#   matrix
#
myvec<-structure(list(lengths = c(1L, 2L, 27L, 1L, 6L, 1L, 15L, 1L, 
                                  23L, 1L, 4L, 2L, 77L, 1L, 1L, 1L, 22L, 2L, 21L, 1L, 3L, 1L, 30L, 
                                  2L, 38L, 1L, 40L, 2L, 22L, 1L, 12L, 1L, 8L, 1L, 12L, 1L, 9L, 
                                  1L, 9L, 1L, 28L, 2L, 12L, 1L, 31L, 1L, 12L, 1L, 5L, 1L, 15L, 
                                  1L, 10L, 1L, 25L, 1L, 16L, 1L, 27L, 1L, 25L, 1L, 31L, 2L, 20L
), values = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
              0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
              1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
              0, 1, 0, 1, 0, 1, 0)), class = "rle")

mymat <- structure(unlist(mapply(rep, myvec$values, myvec$lengths) ), 
.Dim = c(26L, 26L), .Dimnames = list(NULL, NULL) )

colnames(mymat)  <- paste("name", 1:ncol(mymat))
row.names(mymat) <- paste("name", 1:nrow(mymat))

#
# network
#

library(network)
net1 <- network(mymat,
                matrix.type = "adjacency",
                ignore.eval = FALSE
                ,directed=T
)

#
#   Custom position of nodes
#

finalxy<-structure(c(-2, 0, 0, 6, 2, 6, 4, 6, 2, -6, -4, 0, 4, 0, 8, 8, 
                     -4, -2, -2, 8, 10, 2, 0, -4, -2, 4, 4, 2, 4, 4, 0, 2, -2, -2, 
                     2, -2, -2, 0, -4, -2, -4, -2, 0, 0, -2, 0, 0, -2, -4, -4, -4, 
                     4), .Dim = c(26L, 2L) )

net1 %v% "xpos" = finalxy[, 1]
net1 %v% "ypos" = finalxy[, 2]

#
# edges 
#
library(ggnetwork)
library(dplyr)

networkdata <- sapply(net1$mel, function(x) 
  c('id_inl' = x$inl, 'id_outl' = x$outl, 'weight' = x$atl$weights)) %>%
  t %>% as_tibble()

posxyOrig<-finalxy[networkdata$id_outl,]
posxyTarg<-finalxy[networkdata$id_inl,]

colnames(posxyOrig)<-c("xorig","yorig")
colnames(posxyTarg)<-c("xtarg","ytarg")

posData1<-cbind(networkdata,posxyOrig,posxyTarg )

#
# modify arrows (edges) to avoid overlap
#

posData1$xtarg <- ifelse(posData1$xorig>posData1$xtarg,
                        posData1$xtarg+.2,
                        ifelse(posData1$xorig==posData1$xtarg,
                               posData1$xtarg,
                               posData1$xtarg-.2
                        )
)

posData1$ytarg <- ifelse(posData1$yorig>posData1$ytarg,
                        posData1$ytarg+.2,
                        ifelse(posData1$yorig==posData1$ytarg,
                               posData1$ytarg,
                               posData1$ytarg-.2
                        )
)

#
#   plot
#

ggplot(net1 )+
  geom_edges(data=posData1, aes(x = xorig, y = yorig, xend = xtarg, yend = ytarg),
             arrow = arrow(length = unit(10, "pt"), type = "closed")
  ) +
  geom_nodes(aes(x=xpos, y=ypos ), size = 4) +
  geom_nodelabel(aes(x=xpos, y=ypos, label = vertex.names  ),
                 hjust="outward",
                 vjust="outward"
                 ,fontface = "italic", size=3
  ) + theme_blank()

编辑

# related to https://stackoverflow.com/questions/56028991/override-horizontal-positioning-with-ggrepel

ggplot(ggnetwork(net1) )+
geom_edges(data=posData1, aes(x = xorig, y = yorig, xend = xtarg, yend = ytarg),
             arrow = arrow(length = unit(10, "pt"), type = "closed")
  ) +
  geom_nodes(aes(x=xpos, y=ypos ), size = 4) +
  geom_nodelabel_repel(aes(x=xpos, y=ypos, label = vertex.names  ),
                 hjust="outward",
                 vjust="outward"
                 ,fontface = "italic", size=3
  ) + theme_blank()

在此处输入图像描述

4

1 回答 1

0

解决方法:使用ggrepel,向外改为向内

ggplot(ggnetwork(net1 ) )+
  geom_edges(data=posData1, aes(x = xorig, y = yorig, xend = xtarg, yend = ytarg),
             arrow = arrow(length = unit(10, "pt"), type = "closed")
  ) +
  geom_nodes(aes(x=xpos, y=ypos ), size = 4) +
  geom_nodelabel_repel(aes(x=xpos, y=ypos, label = vertex.names  ),
                 hjust="inward",
                 vjust="inward"
                 ,fontface = "italic", size=3
  ) + theme_blank()

在此处输入图像描述

相关: https ://github.com/slowkow/ggrepel/issues/191

于 2020-09-01T23:07:10.403 回答