첫 번째 이미지는 그래프의 손으로 그린 (MS 단어 사용) 이미지입니다. 두 번째 이미지는 ggraph를 사용하여 동일한 그래프를 생성하려는 시도입니다.
다음은 노드 에지 연결이 주어 졌을 때 그래프를 자동으로 그리는 데 사용한 코드입니다 (코드는 이 스레드 에서 채택 됨 ). 손으로 그린 그래프 (이미지 1)와 같이 ggraph의 x 및 y 축을 이동하고 싶습니다. 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()
scale_x_reverse () 사용을 시도했지만 레이아웃이 왜곡되고 " 'x'에 대한 배율이 이미 있습니다. 'x'에 대해 다른 배율을 추가하면 기존 배율을 대체합니다."라는 경고가 표시됩니다. 또한 scale_y_continuous에서 position = "top"옵션을 시도했지만 차이가 나지 않습니다.
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()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다