使用ggdag和ggplot的标签。

huangapple go评论61阅读模式
英文:

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 &lt;- dagify(&quot;a&quot; ~ &quot;b&quot;,
    &quot;b&quot; ~ &quot;c&quot;,
    &quot;c&quot; ~ &quot;d&quot;,
    &quot;b&quot; ~ &quot;d&quot;,
    exposure = &quot;b&quot;,
    outcome = &quot;d&quot;,
    labels = c(a = &quot;A&quot;,
        b = &quot;B&quot;,
        c = &quot;D&quot;))

And with ggplot I can get nice control of the colors such as

test %&gt;%
    ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
        geom_dag_point(color = &quot;orange&quot;) +
        geom_dag_edges_arc(edge_color = &quot;blue&quot;, curvature = 0) +
        geom_dag_text(color = &quot;black&quot;) +
        theme_dag()

使用ggdag和ggplot的标签。

But I can't get it to display labels, such as in

ggdag(test, text = FALSE,
    use_labels = &quot;label&quot;, edge_type = &quot;link_arc&quot;)

使用ggdag和ggplot的标签。

How do I get labels with DAGs and ggplot?

答案1

得分: 1

我发现文档提到了 geom_dag_repel_label,但实际函数应该是 geom_dag_label_repel

test %&gt;%
    ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
        geom_dag_point(color = &quot;orange&quot;) +
        geom_dag_edges_arc(edge_color = &quot;blue&quot;, curvature = 0) +
        geom_dag_label_repel(aes(label = label), colour = &quot;black&quot;, show.legend = FALSE) +
        theme_dag()

使用ggdag和ggplot的标签。

英文:

I figured out that the documentation refers to geom_dag_repel_label but the function is really geom_dag_label_repel

test %&gt;%
    ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
        geom_dag_point(color = &quot;orange&quot;) +
        geom_dag_edges_arc(edge_color = &quot;blue&quot;, curvature = 0) +
        geom_dag_label_repel(aes(label = label), colour = &quot;black&quot;, show.legend = FALSE) +
        theme_dag()

使用ggdag和ggplot的标签。

huangapple
  • 本文由 发表于 2023年7月10日 20:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定