以下代码生成一个有向图,每个节点(START和END节点除外)具有1个入度和1个出度。我可以在有向圆中绘制图形,如图here所示,但我最终希望将其绘制成一条直线。任何帮助将不胜感激。
library(igraph)
g = graph.data.frame(edges)
V(g)$color = ifelse(V(g)$name == START, "green", ifelse(V(g)$name == END, "red", "light blue"))
plot(g, vertex.frame.color='black', vertex.label.font=1, vertex.label.dist=0.5, vertex.label.cex=0.5, vertex.label=NA,
vertex.size=ifelse(V(g)$name == START, 2, ifelse(V(g)$name == END, 2, 1)), edge.arrow.size=0.1, edge.width=E(g)$weights/250, layout=layout.circle,
main=paste('D6 cell ordering network\n',n_cells,' cells', sep=''))
编辑:可重复的例子。
START = "D6_EcadSort2250b_TTCACAAGTTTC"
END = "D6_NoSort2250b_ATTATACGCCCC"
library(TSP)
m <- as.matrix(dist_mat)
S <- which(colnames(m) == START)
E <- which(colnames(m) == END)
atsp <- ATSP(m[-c(S,E), -c(S,E)])
head(atsp)
atsp <- insert_dummy(atsp, label = "S/E")
SE <- which(colnames(atsp) == "S/E")
atsp[SE, ] <- c(m[-c(S,E), S], 0)
atsp[, SE] <- c(m[E, -c(S,E)], 0)
# which method is best?
methods <- c("nearest_insertion", "farthest_insertion", "cheapest_insertion", "arbitrary_insertion", "nn", "repetitive_nn", "two_opt")
tours <- sapply(methods, FUN = function(m) solve_TSP(atsp, method = m), simplify=FALSE)
dotchart(sort(sapply(tours, tour_length)), xlab = "tour length")
best_method = row.names(data.frame((which.min(sort(sapply(tours, tour_length))))))
tour <- solve_TSP(atsp, method=best_method)
path_labels <- c(START, labels(cut_tour(tour, SE)), END)
path_ids <- match(path_labels, colnames(m))
node1 = (path_labels)
node2 = (path_labels[2:length(path_labels)])
length(node2) = length(node1)
edges = data.frame(cbind(node1, node2))
edges = edges[complete.cases(edges),]
weights = m[match(path_labels, rownames(m)),match(path_labels, colnames(m))]
n = 2
n1 = n:ncol(weights)
weights = weights[cbind(seq(n1), n1)]
stopifnot(round(sum(weights), 2) == round(tour_length(tour), 2))
edges$weights = weights
m
:
D6_NoSort2250b_ATTATACGCCCC D6_EcadSort6000b_CCACACCCAGCC D6_EcadSort2250b_CCGATGATTAGC
D6_NoSort2250b_ATTATACGCCCC 0.000 1975.317 1170.9915
D6_EcadSort6000b_CCACACCCAGCC 1975.317 0.000 1977.8852
D6_EcadSort2250b_CCGATGATTAGC 1170.991 1977.885 0.0000
D6_EcadSort2250b_TGTCTGCTTTAG 1106.524 1689.820 962.0281
D6_EcadSort2250b_TTCACAAGTTTC 1022.989 1762.582 1073.0755
D6_EcadSort2250b_TGTCTGCTTTAG D6_EcadSort2250b_TTCACAAGTTTC
D6_NoSort2250b_ATTATACGCCCC 1106.5238 1022.9888
D6_EcadSort6000b_CCACACCCAGCC 1689.8195 1762.5819
D6_EcadSort2250b_CCGATGATTAGC 962.0281 1073.0755
D6_EcadSort2250b_TGTCTGCTTTAG 0.0000 975.8099
D6_EcadSort2250b_TTCACAAGTTTC 975.8099 0.0000
答案 0 :(得分:0)
这有点hacky但似乎有效
首先,我要创建一个非常小的图形,其结构与您描述的结构类似。
g <- graph_from_literal(B-+C, C-+End, Start-+B)
plot(g) # default plot looks nice but not straight
布局只是矩阵,因此我们可以指定一行中的所有内容。这是错误的顺序。
lo <- cbind(seq(-1,1,length.out = gorder(g)), 0)
plot(g, layout = lo) # wrong order
最后一步是使用起始节点的自我图来获得订单
# Find the ordering by using the ego graph of the start node
x <- as.numeric(ego(g, gorder(g), "Start")[[1]])
plot(g, layout = lo[order(x), ])