假设通过R包igraph
可视化的决策树。
library(igraph)
n_of_vertices = 31
g = graph.tree(n_of_vertices, 2)
edge_labels = rep(c("Yes","No"), times=n_of_vertices/2)
plot(g,
layout = layout.reingold.tilford(g, root=1),
asp=0.5,
vertex.shape="square",
vertex.label=NA,
vertex.size=6,
vertex.color="black",
vertex.frame.color="white",
edge.width=3,
edge.arrow.mode=0,
edge.label=edge_labels,
edge.label.family="sans",
edge.label.color="black",
edge.label.cex=0.75)
我正在寻找一种方法在每个顶点行的左侧添加其他标签,例如,“决策1”,“决策2”等。我在下面的示例中添加了红色字体的标签。
答案 0 :(得分:1)
您可以使用text
功能。
## Your tree plot
plot(g,
layout = layout.reingold.tilford(g, root=1),
asp=0.5,
vertex.shape="square",
vertex.label=NA,
vertex.size=6,
vertex.color="black",
vertex.frame.color="white",
edge.width=3,
edge.arrow.mode=0,
edge.label=edge_labels,
edge.label.family="sans",
edge.label.color="black",
edge.label.cex=0.75)
text(x=-1.2, y=c(0.5, 0, -0.5),
labels=c("Decision 1", "Decision 2", "Decision 3"),
col="red")
这里的一个技巧是决定 放置文本。我是如何获得x,y坐标来放置标签的?我用了locator
。这允许您只需单击图形上的某个点并找到其坐标。您可能需要稍微调整一下,或使用cex
调整字体大小。
答案 1 :(得分:0)
magickr
可用于操作绘图并使用文本进行注释。
library(igraph)
library(magick)
n_of_vertices = 31
g = graph.tree(n_of_vertices, 2)
edge_labels = rep(c("Yes","No"), times=n_of_vertices/2)
fig <- image_graph(width = 600, height = 600, res = 96)
#img <- image_draw(frink)
plot(g,
layout = layout.reingold.tilford(g, root=1),
asp=0.5,
vertex.shape="square",
vertex.label=NA,
vertex.size=6,
vertex.color="black",
vertex.frame.color="white",
edge.width=3,
edge.arrow.mode=0,
edge.label=edge_labels,
edge.label.family="sans",
edge.label.color="black",
edge.label.cex=0.75)
dev.off()
out <- fig %>%
image_annotate("Decision Tree", size = 20, color = "black",
degrees = 0, location = "+250+100")
print(out)