0

我需要更改轴中断geom_mosaic。这是我的例子:

my_tibble <-
  tibble(a=rep(c("a1","a2"),each=4),
         b=rep(rep(c("b1","b2"),each=2),times=2),
         c=rep(c("c1","c2"),times=4),
         x=seq_along(b)
  )

my_tibble |>
  ggplot() +
  geom_mosaic(aes(x=product(a,b,c),weight=x,fill=a),
              divider=c("vspine","hspine","hspine"))
  

在此处输入图像描述

我只想要外部分组变量的标签,像这样

在此处输入图像描述

这可以通过简单的函数调用来实现geom_mosaic吗?

4

1 回答 1

0

我根据这篇文章找到了一个解决方案:order and fill with 2 different variables geom_bar ggplot2 R

my_tibble |>
  ggplot() +
  geom_mosaic(aes(x=product(a,b,c),weight=x,fill=a),
              divider=c("vspine","hspine","hspine")) +
  labs(x="c") +
  scale_x_productlist(breaks=my_tibble |>
                        group_by(c) |>
                        summarise(pos = sum(x)) |>
                        mutate(pos=cumsum(pos/sum(pos)),
                               lag = lag(pos)) |>
                        replace_na(list(lag=0)) |>
                        rowwise() |>
                        mutate(posx=sum(pos,lag)/2) |> 
                        pull(posx),
                      labels=my_tibble |>
                        pull(c) |>
                        unique())

代码仍然需要清理。

于 2022-02-15T00:10:11.547 回答