igraph(R)中只有根和终点顶点的标签?

时间:2017-12-04 20:45:09

标签: r igraph labels vertices

div

如何在图表中专门为我的根和终端顶点添加标签?我知道它会涉及这个功能,但你会如何设置它?假设图形对象只是被称为“g'”或者是明显的东西。

$('.wrapper').append('\
                <div id="' + gameId + '" class="main-wrapper '+ gameId +' col-lg-6 col-md-6 col-sm-12">\
                <div class="game-cards">\
                <div class="chart-container">\
                <canvas id="'+ homeTeam +'" width="500" height="500"></canvas>\
                </div>\
                <div class="right-info">\
                <h4>' + awayTeam + '<br>' + " @ " + '<br>' + homeTeam +'</h4>\
                <h5 id="time-channel">'+ gameDate +' @ ' + gameTime  + '<br>' + ' On ' + network +'</h5>\
                <div class="total-points-live">\
                <h5>Total Points Bet</h5>\
                <h5 id="point-total">'+ pointTotal +'</h5>\
                <p>'+ awayTeam +'</p>\
                <input class="bet-input-away" data-team-type="'+ awayTeam +'" type="number" pattern="[0-9]*" name="betAmountAway" placeholder="Wager Amount">\
                <p>'+ homeTeam +'</p>\
                <input class="bet-input-home" data-team-type="'+ homeTeam +'" type="number" pattern="[0-9]*" name="betAmountHome" placeholder="Wager Amount">\
                <p class="bet-button" gameid="'+ gameId +'">Click To Place Bet</p>\
                </div>\
                </div>\
                </div>\
');



$('.wrapper').on('click', '.bet-button', function() {
    var self = $(this);
    var gameId = self.attr('gameid');
    var awayVal = $('#' + gameId + ' input[name=betAmountAway]');
    var homeVal = $('#' + gameId + ' input[name=betAmountHome]');
    console.log(gameId);
    console.log(homeVal);
    console.log(awayVal);
});

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

来自@ eipi1o的解决方案很好,但OP说“我发现很难有效地应用于我的大型数据集。”#34;我怀疑问题是找到哪些是中间节点,其名称应该被消除。我将继续@ eipi10的例子。由于我的答案是基于他的,如果你赞成我的答案,请同时投票给他。

您可以使用neighbors功能确定哪些点是源和汇。其他一切都是中间节点。

## 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