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

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

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

问题

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

library(ggparty)
#> Loading required package: ggplot2
#> Loading required package: partykit
#> Loading required package: grid
#> Loading required package: libcoin
#> Loading required package: mvtnorm
sp_o <- partysplit(1L, index = 1:3)
n1 <- partynode(id = 1L, split = sp_o, kids = lapply(2L:4L, partynode))
t2 <- party(n1,
            data = WeatherPlay,
            fitted = data.frame(
              "(fitted)" = fitted_node(n1, data = WeatherPlay),
              "(response)" = WeatherPlay$play,
              check.names = FALSE),
            terms = terms(play ~ ., data = WeatherPlay)
)
ggparty(t2) +
  geom_edge() +
  geom_edge_label() +
  geom_node_splitvar() +
  # 通过传递包含要为每个节点绘制的所有 ggplot 组件的 gglist 列表
  #(默认:终端)节点
  geom_node_plot(gglist = list(geom_bar(aes(x = "", fill = play),
                                        position = position_fill()),
                               xlab("play") +
                                 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:

library(ggparty)
#&gt; Loading required package: ggplot2
#&gt; Loading required package: partykit
#&gt; Loading required package: grid
#&gt; Loading required package: libcoin
#&gt; Loading required package: mvtnorm
sp_o &lt;- partysplit(1L, index = 1:3)
n1 &lt;- partynode(id = 1L, split = sp_o, kids = lapply(2L:4L, partynode))
t2 &lt;- party(n1,
            data = WeatherPlay,
            fitted = data.frame(
              &quot;(fitted)&quot; = fitted_node(n1, data = WeatherPlay),
              &quot;(response)&quot; = WeatherPlay$play,
              check.names = FALSE),
            terms = terms(play ~ ., data = WeatherPlay)
)
ggparty(t2) +
  geom_edge() +
  geom_edge_label() +
  geom_node_splitvar() +
  # pass list to gglist containing all ggplot components we want to plot for each
  # (default: terminal) node
  geom_node_plot(gglist = list(geom_bar(aes(x = &quot;&quot;, fill = play),
                                        position = position_fill()),
                               xlab(&quot;play&quot;) +
                                 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公式列表中删除“+”并替换为“,”:

library(ggparty)
#> Loading required package: ggplot2
#> Loading required package: partykit
#> Loading required package: grid
#> Loading required package: libcoin
#> Loading required package: mvtnorm
sp_o <- partysplit(1L, index = 1:3)
n1 <-
  partynode(id = 1L,
            split = sp_o,
            kids = lapply(2L:4L, partynode))
t2 <- party(
  n1,
  data = WeatherPlay,
  fitted = data.frame(
    "(fitted)" = fitted_node(n1, data = WeatherPlay),
    "(response)" = WeatherPlay$play,
    check.names = FALSE
  ),
  terms = terms(play ~ ., data = WeatherPlay)
)
ggparty(t2) ,
  geom_edge() ,
  geom_edge_label() ,
  geom_node_splitvar() ,
  # 将包含我们要为每个(默认:终端)节点绘制的所有ggplot组件的列表传递给gglist
  geom_node_plot(gglist = list(
    geom_bar(aes(x = "", fill = play),
             position = position_fill()),
    xlab("play") ,
      scale_fill_manual(values = c("lightblue", "blue"))
  ))

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

英文:

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

library(ggparty)
#&gt; Loading required package: ggplot2
#&gt; Loading required package: partykit
#&gt; Loading required package: grid
#&gt; Loading required package: libcoin
#&gt; Loading required package: mvtnorm
sp_o &lt;- partysplit(1L, index = 1:3)
n1 &lt;-
  partynode(id = 1L,
            split = sp_o,
            kids = lapply(2L:4L, partynode))
t2 &lt;- party(
  n1,
  data = WeatherPlay,
  fitted = data.frame(
    &quot;(fitted)&quot; = fitted_node(n1, data = WeatherPlay),
    &quot;(response)&quot; = WeatherPlay$play,
    check.names = FALSE
  ),
  terms = terms(play ~ ., data = WeatherPlay)
)
ggparty(t2) +
  geom_edge() +
  geom_edge_label() +
  geom_node_splitvar() +
  # pass list to gglist containing all ggplot components we want to plot for each
  # (default: terminal) node
  geom_node_plot(gglist = list(
    geom_bar(aes(x = &quot;&quot;, fill = play),
             position = position_fill()),
    xlab(&quot;play&quot;) ,
      scale_fill_manual(values = c(&quot;lightblue&quot;, &quot;blue&quot;))
  ))

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:

确定