ggparty::geom_node_plot()是否可以更改颜色比例?

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

Is it possible to change the color scale with ggparty::geom_node_plot()?

问题

我希望能够使用 scale_fill_manual() 来更改此图中的填充颜色,但它不起作用:

  1. library(ggparty)
  2. #> Loading required package: ggplot2
  3. #> Loading required package: partykit
  4. #> Loading required package: grid
  5. #> Loading required package: libcoin
  6. #> Loading required package: mvtnorm
  7. sp_o <- partysplit(1L, index = 1:3)
  8. n1 <- partynode(id = 1L, split = sp_o, kids = lapply(2L:4L, partynode))
  9. t2 <- party(n1,
  10. data = WeatherPlay,
  11. fitted = data.frame(
  12. "(fitted)" = fitted_node(n1, data = WeatherPlay),
  13. "(response)" = WeatherPlay$play,
  14. check.names = FALSE),
  15. terms = terms(play ~ ., data = WeatherPlay)
  16. )
  17. ggparty(t2) +
  18. geom_edge() +
  19. geom_edge_label() +
  20. geom_node_splitvar() +
  21. # 通过传递包含要为每个节点绘制的所有 ggplot 组件的 gglist 列表
  22. #(默认:终端)节点
  23. geom_node_plot(gglist = list(geom_bar(aes(x = "", fill = play),
  24. position = position_fill()),
  25. xlab("play") +
  26. scale_fill_manual(values = c("lightblue", "blue"))))

ggparty::geom_node_plot()是否可以更改颜色比例?<!-- -->

<sup>此代码来自 ggparty "Graphic Partying" 详细介绍 的“节点图”部分,除了 scale_fill_manual() 这一行。</sup>

英文:

I would expect to be able to change the fill colors in this plot with scale_fill_manual() but it doesn't work:

  1. library(ggparty)
  2. #&gt; Loading required package: ggplot2
  3. #&gt; Loading required package: partykit
  4. #&gt; Loading required package: grid
  5. #&gt; Loading required package: libcoin
  6. #&gt; Loading required package: mvtnorm
  7. sp_o &lt;- partysplit(1L, index = 1:3)
  8. n1 &lt;- partynode(id = 1L, split = sp_o, kids = lapply(2L:4L, partynode))
  9. t2 &lt;- party(n1,
  10. data = WeatherPlay,
  11. fitted = data.frame(
  12. &quot;(fitted)&quot; = fitted_node(n1, data = WeatherPlay),
  13. &quot;(response)&quot; = WeatherPlay$play,
  14. check.names = FALSE),
  15. terms = terms(play ~ ., data = WeatherPlay)
  16. )
  17. ggparty(t2) +
  18. geom_edge() +
  19. geom_edge_label() +
  20. geom_node_splitvar() +
  21. # pass list to gglist containing all ggplot components we want to plot for each
  22. # (default: terminal) node
  23. geom_node_plot(gglist = list(geom_bar(aes(x = &quot;&quot;, fill = play),
  24. position = position_fill()),
  25. xlab(&quot;play&quot;) +
  26. scale_fill_manual(values = c(&quot;lightblue&quot;, &quot;blue&quot;))))

ggparty::geom_node_plot()是否可以更改颜色比例?<!-- -->

<sup>Created on 2023-03-03 with reprex v2.0.2</sup>

This code is from the Node Plot section of the ggparty "Graphic Partying" vignette with the exception of the scale_fill_manual() line.

答案1

得分: 1

你应该在你的ggplot公式列表中删除“+”并替换为“,”:

  1. library(ggparty)
  2. #> Loading required package: ggplot2
  3. #> Loading required package: partykit
  4. #> Loading required package: grid
  5. #> Loading required package: libcoin
  6. #> Loading required package: mvtnorm
  7. sp_o <- partysplit(1L, index = 1:3)
  8. n1 <-
  9. partynode(id = 1L,
  10. split = sp_o,
  11. kids = lapply(2L:4L, partynode))
  12. t2 <- party(
  13. n1,
  14. data = WeatherPlay,
  15. fitted = data.frame(
  16. "(fitted)" = fitted_node(n1, data = WeatherPlay),
  17. "(response)" = WeatherPlay$play,
  18. check.names = FALSE
  19. ),
  20. terms = terms(play ~ ., data = WeatherPlay)
  21. )
  22. ggparty(t2) ,
  23. geom_edge() ,
  24. geom_edge_label() ,
  25. geom_node_splitvar() ,
  26. # 将包含我们要为每个(默认:终端)节点绘制的所有ggplot组件的列表传递给gglist
  27. geom_node_plot(gglist = list(
  28. geom_bar(aes(x = "", fill = play),
  29. position = position_fill()),
  30. xlab("play") ,
  31. scale_fill_manual(values = c("lightblue", "blue"))
  32. ))

不要删除“+”或者“,”前面的空格,因为它们在代码中起着重要的分隔作用。

英文:

You should remove the "+" and replace by "," in your ggplot formula list :

  1. library(ggparty)
  2. #&gt; Loading required package: ggplot2
  3. #&gt; Loading required package: partykit
  4. #&gt; Loading required package: grid
  5. #&gt; Loading required package: libcoin
  6. #&gt; Loading required package: mvtnorm
  7. sp_o &lt;- partysplit(1L, index = 1:3)
  8. n1 &lt;-
  9. partynode(id = 1L,
  10. split = sp_o,
  11. kids = lapply(2L:4L, partynode))
  12. t2 &lt;- party(
  13. n1,
  14. data = WeatherPlay,
  15. fitted = data.frame(
  16. &quot;(fitted)&quot; = fitted_node(n1, data = WeatherPlay),
  17. &quot;(response)&quot; = WeatherPlay$play,
  18. check.names = FALSE
  19. ),
  20. terms = terms(play ~ ., data = WeatherPlay)
  21. )
  22. ggparty(t2) +
  23. geom_edge() +
  24. geom_edge_label() +
  25. geom_node_splitvar() +
  26. # pass list to gglist containing all ggplot components we want to plot for each
  27. # (default: terminal) node
  28. geom_node_plot(gglist = list(
  29. geom_bar(aes(x = &quot;&quot;, fill = play),
  30. position = position_fill()),
  31. xlab(&quot;play&quot;) ,
  32. scale_fill_manual(values = c(&quot;lightblue&quot;, &quot;blue&quot;))
  33. ))

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

发表评论

匿名网友

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

确定