R-在多个图中创建相同的冲积顺序

时间:2019-11-22 17:16:44

标签: r ggplot2

我正在使用ggalluvial软件包为我拥有的某些数据制作多个冲积图,但我想尝试订购这些冲积图,以便可以在多个图之间进行比较。

以下是一些示例数据:

set.seed(234)

Data1 <- data.frame(
  ID = rep(1:10, each = 6),
  Group = rep(1:2, each = 30),
  Week = rep(1:6, times = 10),
  Y = sample(c("High", "Low", "None"), 60, replace = TRUE)
)

Data2 <- data.frame(
  ID = rep(1:10, each = 6),
  Group = rep(1:2, each = 30),
  Week = rep(1:6, times = 10),
  Y = sample(c("High", "Low", "None"), 60, replace = TRUE)
)

以及制作两个图形的一些示例代码:

plot1 <- ggplot(Data1,
       aes(x = Week, 
           stratum = Y, 
           alluvium = ID,
           fill = Y,
           label = Y))+
  facet_grid(Group ~.)+
  scale_fill_manual(values = c("red", "yellow", "green3"))+
  geom_flow(stat = "alluvium", lode.guidance = "frontback", color = "darkgray")+
  geom_stratum()

plot2 <- ggplot(Data2,
       aes(x = Week, 
           stratum = Y, 
           alluvium = ID,
           fill = Y,
           label = Y))+
  facet_grid(Group ~.)+
  scale_fill_manual(values = c("red", "yellow", "green3"))+
  geom_flow(stat = "alluvium", lode.guidance = "frontback", color = "darkgray")+
  geom_stratum()

最后是两个输出图 Plot1 Plot2

有没有办法知道哪个冲积物属于哪个人?或指定冲积的顺序,例如,第一组和第二张图中的第一组冲积是相同的?我意识到这可能会使图形看起来有些糟糕,但是对于我的实际数据,第一周的结果在各个方面都相当均匀,因此我认为这是可以的。预先感谢。

1 个答案:

答案 0 :(得分:2)

这时对我而言最有意义的方法是,使用新的层将alluvium变量(在这种情况下为ID)传递给label美学。冲积统计和文本几何。如果参数(例如lode.guidance)被传递相同的值,则新层应在每个轴上以相同顺序堆叠Alluvia,并且文本标签将与Allovia与地层相交的“凸角”重合。

这是您关于附加层的示例。注意,它是在 层之后添加的;否则文本将被填充的矩形遮盖。

library(ggalluvial)
#> Loading required package: ggplot2

set.seed(234)

Data1 <- data.frame(
  ID = rep(1:10, each = 6),
  Group = rep(1:2, each = 30),
  Week = rep(1:6, times = 10),
  Y = sample(c("High", "Low", "None"), 60, replace = TRUE)
)

Data2 <- data.frame(
  ID = rep(1:10, each = 6),
  Group = rep(1:2, each = 30),
  Week = rep(1:6, times = 10),
  Y = sample(c("High", "Low", "None"), 60, replace = TRUE)
)

ggplot(Data1,
       aes(x = Week, 
           stratum = Y, 
           alluvium = ID,
           fill = Y,
           label = Y))+
  facet_grid(Group ~.)+
  scale_fill_manual(values = c("red", "yellow", "green3"))+
  geom_flow(stat = "alluvium", lode.guidance = "frontback", color = "darkgray")+
  geom_stratum()+
  geom_text(stat = "alluvium", aes(label = ID), lode.guidance = "frontback")


ggplot(Data2,
       aes(x = Week, 
           stratum = Y, 
           alluvium = ID,
           fill = Y,
           label = Y))+
  facet_grid(Group ~.)+
  scale_fill_manual(values = c("red", "yellow", "green3"))+
  geom_flow(stat = "alluvium", lode.guidance = "frontback", color = "darkgray")+
  geom_stratum()+
  geom_text(stat = "alluvium", aes(label = ID), lode.guidance = "frontback")

reprex package(v0.3.0)于2019-11-22创建