2017-12-04 20 views
2
inst2 = c(2, 3, 4, 5, 6) 
motherinst2 = c(7, 8, 2, 10, 11) 
km = c(20, 30, 40, 25, 60) 
df2 = data.frame(inst2, motherinst2) 
df2 = cbind(df2, km) 
g2 = graph_from_data_frame(df2) 
tkplot(g2) 

그래프에 루트 및 터미널 정점에만 레이블을 추가하는 방법은 무엇입니까? 나는 그것이이 기능을 포함 할 것이라는 것을 안다, 그러나 당신은 어떻게 그것을 설정할 것입니까? 가정하면 그래프 객체는 'g'라고 부르기도하고 명백한 것입니다.igraph (R)의 루트 및 터미널 꼭지점에 대한 레이블?

vertex.label = 
+1

당신은 작업 할 샘플 그래프를 제공 할 수 있습니까? – eipi10

+0

예제 그래프에는 루트 중간 터미널 구조가 아니라 연결된 꼭지점 쌍만 있습니다. – eipi10

+0

안녕하세요. 죄송합니다. 샘플 세트를 고정시켜 중간 정점을 갖도록했습니다. 귀하의 예를 들어 주시면 감사하겠습니다 만, 제 대규모 데이터 세트에 효과적으로 적용하기가 어렵습니다. – hmnoidk

답변

2

예제 그래프를 사용하여 루트 및 터미널 정점을 식별하고 다른 정점의 레이블을 제거합니다. 여기에 초기 그래프의 모습입니다 : 이제

set.seed(2) 
plot(g2) 

enter image description here

하는의는 중간 정점

# Get all edges 
e = get.edgelist(g2) 

# Root vertices are in first column but not in second column 
root = setdiff(e[,1],e[,2]) 

# Terminal vertices are in second column but not in first column 
terminal = setdiff(e[,2], e[,1]) 

# Vertices to remove are not in root or terminal vertices 
remove = setdiff(unique(c(e)), c(root, terminal)) 

# Remove names of intermediate vertices 
V(g2)$name[V(g2)$name %in% remove] = "" 

set.seed(2) 
plot(g2) 

enter image description here

원래 응답의 이름을 식별하고 제거 할 수

set.vertex.attribute을 사용하여 레이블 이름을 변경할 수 있습니다.

library(igraph) 

# Create a graph to work with 
g = graph_from_edgelist(cbind(c(rep(1,10),2:11), c(2:21))) 

plot(g) 

enter image description here

이제 우리는 중간 정점에서 라벨을 제거 할 수 있습니다 : 예를 들면 다음과 같습니다이다

g = set.vertex.attribute(g, "name", value=c(1,rep("", length(2:11)),12:21)) 

plot(g) 

enter image description here

2
@ eipi1o에서이 솔루션은 좋은

하지만, OP "대용량 데이터 세트에 효과적으로 적용하기가 어렵습니다." 그 문제는 이름이 공백으로 표시되어야하는 중개 노드를 찾는 것이 아닌가 의심됩니다. @ eipi10의 예를 계속하겠습니다. 내 답변이 그의 대답을 기반으로하기 때문에, 내 대답을 upvote면, 그의 upvote하시기 바랍니다.

기능을 사용하여 소스 및 싱크 지점을 확인할 수 있습니다. 다른 모든 것은 중간 노드입니다.

## original graph from eipi10 
g = graph_from_edgelist(cbind(c(rep(1,10),2:11), c(2:21))) 

## Identify which nodes are intermediate 
SOURCES = which(sapply(V(g), function(x) length(neighbors(g, x, mode="in"))) == 0) 
SINKS = which(sapply(V(g), function(x) length(neighbors(g, x, mode="out"))) == 0) 
INTERMED = setdiff(V(g), c(SINKS, SOURCES)) 

## Fix up the node names and plot 
V(g)$name = V(g) 
V(g)$name[INTERMED] = "" 
plot(g) 

Labeled Graph