我想用R中的DiagrammeR包绘制一个水平图形,但是我发现只绘制了一个垂直图形。你知道怎么把它翻90度吗?
library(DiagrammeR)
library(dplyr)
create_graph() %>%
add_nodes_from_table(table=n,label_col = task) %>%
add_edges_from_table(table=e,from_col = from,to_col = to,from_to_map = label) %>%
set_node_attrs(
node_attr = "shape",
values = "square"
) %>%
render_graph(layout = "tree")结果:

dput:
n <- structure(list(task = c("1", "2", "3", "4", "5", "6", "7", "8",
"A", "B", "C")), .Names = "task", row.names = c(NA, -11L), class = "data.frame")
e <- structure(list(from = c("A", "1", "2", "4", "B", "3", "C", "5"
), to = c("1", "2", "4", "8", "3", "6", "5", "7")), .Names = c("from",
"to"), row.names = c(NA, -8L), class = "data.frame")发布于 2019-01-09 20:16:50
我只使用了我之前的一个模板来说明另一种选择:
grViz("
digraph Random{
graph [layout = circo,
overlap =T,
outputorder = edgesfirst,
bgcolor='white',
splines=line]#controls l type setup
edge[labelfontname='Arial',fontSize=13,color='red',fontcolor='navy']
node [shape = box,style='filled',
fillcolor='indianred4',width=2.5,
fontSize=20,fontcolor='snow',
fontname='Arial']#node shape
a [label = 'A']
b [label = 'B']
c [label='D']
a->b[color='red']
b->c[color='dodgerblue']
}")输出:

发布于 2019-06-14 10:45:47
我发现的唯一方法是在将dot格式传递给grViz之前对其进行操作。在这里,我将默认布局选项替换为dot布局,并通过添加rankdir = LR来反转它。
DiagrammeR::generate_dot(graph) %>%
gsub(pattern = 'neato',replacement = 'dot',x= .) %>%
gsub(pattern = "graph \\[",'graph \\[rankdir = LR,\n',x = .)%>%
grViz所以在你的情况下
n <- structure(list(task = c("1", "2", "3", "4", "5", "6", "7", "8",
"A", "B", "C")), .Names = "task", row.names = c(NA, -11L), class = "data.frame")
e <- structure(list(from = c("A", "1", "2", "4", "B", "3", "C", "5"
), to = c("1", "2", "4", "8", "3", "6", "5", "7")), .Names = c("from",
"to"), row.names = c(NA, -8L), class = "data.frame")
create_graph() %>%
add_nodes_from_table(table=n,label_col = task) %>%
add_edges_from_table(table=e,from_col = from,to_col = to,from_to_map = label) %>%
set_node_attrs(
node_attr = "shape",
values = "square",
) %>%
set_node_attrs(
node_attr = 'fontcolor',
values = 'black'
) %>%
generate_dot() %>%
gsub(pattern = 'neato',replacement = 'dot',x= .) %>%
gsub(pattern = "graph \\[",'graph \\[rankdir = LR,\n',x = .,perl = TRUE) %>%
grViz()

注意,我添加了另一个set_node_attrs来显式地将字体颜色设置为黑色。否则,默认字体颜色为浅灰色。
https://stackoverflow.com/questions/54109621
复制相似问题