ggplot ggmosic geom_mosaic текст для оси x с вложенными
Мне нужно изменить ось в разрывах. Вот мой пример:
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
?
1 ответ
Я нашел решение, основанное на этом сообщении: порядок и заполнение двумя разными переменными 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())
Код все еще нуждается в очистке.