ggalluvial-在轴上放置一些观测值

时间:2019-07-02 11:16:24

标签: r sankey-diagram

我正在尝试使用ggalluvial包创建冲积图。

我的数据包含几个组,这些组中的一个组比其余组大,并且只在图中4轴的前2个中有意义。

小组之间的相对大小差异使得难以阅读作为关注重点的小组。

是否可以从y和z轴上删除“ C”值?

要绘制的数据:

library(dplyr)
set.seed(1)
data_to_plot <- tibble(
  t = "origin", 
  x = sample(c("A", "B", "C"), size = 10000, 
             prob = c(0.1, 0.2, 0.7), replace = TRUE)) %>%
  mutate(y = case_when(
    x == "A" ~ sample(c("A1", "A2", "A3"), size = n(), 
                      prob = c(0.3, 0.4, 0.3), replace = TRUE),
    x == "B" ~ sample(c("B1", "B2", "B3"), size = n(), 
                      prob = c(0.3, 0.4, 0.3), replace = TRUE),
    x == "C" ~ "C")) %>% 
  mutate(z = case_when(
    y == "A1" ~ sample(c("UP", "DOWN", "FORWARD"), size = n(), prob = c(0.4, 0.5, 0.1), replace = TRUE),
    y == "A2" ~ sample(c("UP", "DOWN", "FORWARD"), size = n(), prob = c(0.4, 0.5, 0.1), replace = TRUE),
    y == "A3" ~ sample(c("UP", "DOWN", "FORWARD"), size = n(), prob = c(0.4, 0.5, 0.1), replace = TRUE),
    y == "B1" ~ sample(c("UP", "DOWN", "FORWARD"), size = n(), prob = c(0.4, 0.5, 0.1), replace = TRUE),
    y == "B2" ~ sample(c("UP", "DOWN", "FORWARD"), size = n(), prob = c(0.4, 0.5, 0.1), replace = TRUE),
    y == "B3" ~ sample(c("UP", "DOWN", "FORWARD"), size = n(), prob = c(0.4, 0.5, 0.1), replace = TRUE),
    y == "C" ~ "C")) %>% 
  group_by(t, x, y, z) %>% 
  count()

head(data_to_plot)

# A tibble: 6 x 5
# Groups:   t, x, y, z [6]
  t      x     y     z           n
  <chr>  <chr> <chr> <chr>   <int>
1 origin A     A1    DOWN      160
2 origin A     A1    FORWARD    29
3 origin A     A1    UP        108
4 origin A     A2    DOWN      203
5 origin A     A2    FORWARD    34
6 origin A     A2    UP        170

创建绘图的代码:

data_to_plot %>% 
  ggplot(aes(y = n, axis1 = t, axis2 = x, axis3 = y, axis4 = z))+
  geom_alluvium(aes(fill = z), width = 0.6) +
  geom_stratum(alpha = 0.1, width = 0.6)+
  ggfittext::geom_fit_text(stat = "stratum", label.strata = TRUE, width = 0.6, min.size = 1)+
  scale_x_discrete(limits = c("start", "x", "y", "z"), expand = c(.05, .05))+
  scale_fill_brewer(type = "qual", palette = "Set1")

在y和z轴上显示(不重要)“ C”值的图 enter image description here

0 个答案:

没有答案