使用ggdag和ggplot的标签。

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

Use labels with ggdag and ggplot

问题

我正在使用ggdag创建一个有向无环图(DAG),如下所示:

  1. test <- dagify("a" ~ "b",
  2. "b" ~ "c",
  3. "c" ~ "d",
  4. "b" ~ "d",
  5. exposure = "b",
  6. outcome = "d",
  7. labels = c(a = "A",
  8. b = "B",
  9. c = "D"))

然后,使用ggplot可以轻松控制颜色,例如:

  1. test %>%
  2. ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
  3. geom_dag_point(color = "orange") +
  4. geom_dag_edges_arc(edge_color = "blue", curvature = 0) +
  5. geom_dag_text(color = "black") +
  6. theme_dag()

但是,我无法使标签显示出来,就像这样:

  1. ggdag(test, text = FALSE,
  2. use_labels = "label", edge_type = "link_arc")

如何在DAG和ggplot中添加标签呢?

英文:

I'm using ggdag to create a DAG such as

  1. test &lt;- dagify(&quot;a&quot; ~ &quot;b&quot;,
  2. &quot;b&quot; ~ &quot;c&quot;,
  3. &quot;c&quot; ~ &quot;d&quot;,
  4. &quot;b&quot; ~ &quot;d&quot;,
  5. exposure = &quot;b&quot;,
  6. outcome = &quot;d&quot;,
  7. labels = c(a = &quot;A&quot;,
  8. b = &quot;B&quot;,
  9. c = &quot;D&quot;))

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

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

使用ggdag和ggplot的标签。

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

  1. ggdag(test, text = FALSE,
  2. 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

  1. test %&gt;%
  2. ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
  3. geom_dag_point(color = &quot;orange&quot;) +
  4. geom_dag_edges_arc(edge_color = &quot;blue&quot;, curvature = 0) +
  5. geom_dag_label_repel(aes(label = label), colour = &quot;black&quot;, show.legend = FALSE) +
  6. 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

  1. test %&gt;%
  2. ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
  3. geom_dag_point(color = &quot;orange&quot;) +
  4. geom_dag_edges_arc(edge_color = &quot;blue&quot;, curvature = 0) +
  5. geom_dag_label_repel(aes(label = label), colour = &quot;black&quot;, show.legend = FALSE) +
  6. 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:

确定