向igraph的决策树添加多个额外标签
假设通过R包向igraph的决策树添加多个额外标签,r,label,igraph,decision-tree,R,Label,Igraph,Decision Tree,假设通过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", verte
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)
我正在寻找一种方法,在每个顶点行的左侧添加其他标签,例如,“Decision 1”、“Decision 2”等。我在下面的示例中添加了红色字体的标签
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)
库(igraph)
图书馆(magick)
n个顶点中的n个=31
g=图.树(n个顶点,2个)
边标签=代表(c(“是”、“否”),时间=n个顶点/2)
图您可以使用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坐标来放置标签的?我使用了定位器
。这样,您只需单击图形上的某个点,即可找到其坐标。您可能需要稍微调整或使用cex
调整字体大小。我无法重新创建您建议的解决方案。使用您提供的代码时,我收到的只是一张空白图像,上面有文本“Decision tree”。该文本没有覆盖决策树。一个简单明了的解决方案!谢谢你的回答。