英文:
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"))))
<!-- -->
<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)
#> 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() +
  # 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 = "", fill = play),
                                        position = position_fill()),
                               xlab("play") +
                                 scale_fill_manual(values = c("lightblue", "blue"))))
<!-- -->
<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)
#> 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() +
  # 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 = "", fill = play),
             position = position_fill()),
    xlab("play") ,
      scale_fill_manual(values = c("lightblue", "blue"))
  ))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论