英文:
Use labels with ggdag and ggplot
问题
我正在使用ggdag创建一个有向无环图(DAG),如下所示:
test <- dagify("a" ~ "b",
"b" ~ "c",
"c" ~ "d",
"b" ~ "d",
exposure = "b",
outcome = "d",
labels = c(a = "A",
b = "B",
c = "D"))
然后,使用ggplot可以轻松控制颜色,例如:
test %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point(color = "orange") +
geom_dag_edges_arc(edge_color = "blue", curvature = 0) +
geom_dag_text(color = "black") +
theme_dag()
但是,我无法使标签显示出来,就像这样:
ggdag(test, text = FALSE,
use_labels = "label", edge_type = "link_arc")
如何在DAG和ggplot中添加标签呢?
英文:
I'm using ggdag to create a DAG such as
test <- dagify("a" ~ "b",
"b" ~ "c",
"c" ~ "d",
"b" ~ "d",
exposure = "b",
outcome = "d",
labels = c(a = "A",
b = "B",
c = "D"))
And with ggplot I can get nice control of the colors such as
test %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point(color = "orange") +
geom_dag_edges_arc(edge_color = "blue", curvature = 0) +
geom_dag_text(color = "black") +
theme_dag()
But I can't get it to display labels, such as in
ggdag(test, text = FALSE,
use_labels = "label", edge_type = "link_arc")
How do I get labels with DAGs and ggplot?
答案1
得分: 1
我发现文档提到了 geom_dag_repel_label
,但实际函数应该是 geom_dag_label_repel
。
test %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point(color = "orange") +
geom_dag_edges_arc(edge_color = "blue", curvature = 0) +
geom_dag_label_repel(aes(label = label), colour = "black", show.legend = FALSE) +
theme_dag()
英文:
I figured out that the documentation refers to geom_dag_repel_label
but the function is really geom_dag_label_repel
test %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point(color = "orange") +
geom_dag_edges_arc(edge_color = "blue", curvature = 0) +
geom_dag_label_repel(aes(label = label), colour = "black", show.legend = FALSE) +
theme_dag()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论