使用R美化和排序Sankey/Alluvial图中的一些变量

ymzxtsji  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(123)

我正在努力提高我在数据可视化方面的技能,我几乎得到了我想要的。但在某个时候,我被卡住了,不能再往前走了。只是要知道,伙计们,我在这里做了广泛的研究,试图找到我的疑虑,它帮助了我很多。
以下是我的数据集:
https://app.box.com/s/pp5p5chgypn6ba33anotie7wlxvdu01v
下面是我的代码:

library(tidyverse)
library(ggalluvial)
library(alluvial)

A_col <- "firebrick3"
B_col <- "darkorange"
C_col <- "aquamarine2"
D_col <- "dodgerblue2"
E_col <- "darkviolet"
F_col <- "chartreuse2"
G_col <- "goldenrod1"
H_col <- "gray73"
set.seed(39)

ggplot(df,
       aes(y = Time, axis1 = Activity, axis2 = Category, axis3 = Positions)) +
  geom_alluvium(aes(fill = Positions, color = Positions), 
        width = 4/12, alpha = 0.5, knot.pos = 0.3) +
  geom_stratum(width = 4/12, color = "grey36") +
  geom_text(stat = "stratum", label.strata = TRUE) +
  scale_x_continuous(breaks = 1:3, 
       labels = c("Activity", "Category", "Positions/Movements"), expand = c(.01, .05)) +
  ylab("Time 24 hours") +
  scale_fill_manual(values  = c(A_col, B_col, C_col, D_col, E_col, F_col, G_col, H_col)) +
  scale_color_manual(values = c(A_col, B_col, C_col, D_col, E_col, F_col, G_col, H_col)) +
  ggtitle("Physical Activity during the week and weekend") +
  theme_minimal() +
  theme(legend.position = "none", panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), axis.text.y = element_blank(), 
        axis.text.x = element_text(size = 12, face = "bold"))

# I also have this code that I run without pre-choosing the colours.
# I like this one because the flow diagram doesn't have any border.

ggplot(df,
       aes(y = Time, axis1 = Activity, axis2 = Category, axis3 = Positions)) +
  scale_x_discrete(limits = c("Activity", "Category", "Positions/Moviments"), 
       expand = c(.01, .05)) +
  ylab("Time 24 hours") +
  geom_alluvium(aes(fill = Positions), width = 4/12, alpha = 0.5, knot.pos = 0.3) +
  geom_stratum() + geom_text(stat = "stratum", label.strata = TRUE) +
  theme_minimal() +
  ggtitle("Physical Activity during the week and weekend") +
  theme(legend.position = "none", panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), axis.text.y = element_blank(), 
        axis.text.x = element_text(size = 12, face = "bold"))

字符串
以下是可视化:x1c 0d1x的数据
有三件事我真的做不到:
1.对Category进行排序,清晰地看到一周和周末之后,例如WorkingNon WorkingSleep WeekLeisureSleep Weekend
1.对位置/移动进行排序,如SittingLyingStandingMovingStairsWalk SlowWalk FastRunning。另外,我想用流程图的相同颜色填充此列的正方形。另一件事是,有些名字没有足够的空间,我不知道是否有可能重置空间来容纳他们,或者也许把他们外面的箭头指示属于他们的广场。差点忘了,有没有办法手动为每个变量分配颜色,比如colour blackWalk Slow?另外,如果可能的话,我想去掉流程图边缘的线条。
1.有没有办法把位置和动作的名字叠加起来?
有什么方法可以改善这种可视化并使其变得美丽吗?
先谢谢你了,路易斯

m1m5dgzv

m1m5dgzv1#

这里有一个解决方案,可以解决您的一些问题。

df <- read_csv('Desktop/plot_alluvial_category_position_plus_moviments.csv')
positions <- c("Sitting", "Lying", "Standing", "Moving", "Stairs", "Walk Slow",
               "Walk Fast", "Running")
df$Positions <- factor(df$Positions, levels = positions, labels = positions)
category <- c("Working", "Non Working", "Sleep Week", "Leisure", 
              "Sleep Weekend")
df$Category <- factor(df$Category, levels = category, labels = category)

ggplot(df,
       aes(y = Time, axis1 = Activity, axis2 = Category, axis3 = Positions)) +
  geom_alluvium(aes(fill = Positions), 
                width = 4/12, alpha = 0.5, knot.pos = 0.3) +
  geom_stratum(width = 4/12, color = "grey36") +
  geom_text(stat = "stratum", label.strata = TRUE, min.height=100) +
  scale_x_continuous(breaks = 1:3, 
                     labels = c("Activity", "Category", "Positions\nMovements"), expand = c(.01, .05)) +
  ylab("Time 24 hours") +
  scale_fill_manual(values  = c(A_col, B_col, C_col, D_col, E_col, F_col, G_col, H_col)) +
  scale_color_manual(values = c(A_col, B_col, C_col, D_col, E_col, F_col, G_col, H_col)) +
  ggtitle("Physical activity during the week and weekend") +
  theme_minimal() +
  theme(legend.position = "none", panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), axis.text.y = element_blank(), 
        axis.text.x = element_text(size = 12, face = "bold"))

字符串
1.要对层进行排序,需要将CategoryPosition列转换为因子,在因子中设置水平顺序。
1.要删除流程图的边缘,只需从aes级别中删除color = Position即可。
1.您可以通过在标签中添加换行符来堆叠名称Position和Movement。
1.您可以将颜色分配给层,但前提是类别始终相同(请查看ggalluvial文档中的一些示例)。
1.为了避免小层中的重叠,可以在geom_text中使用min.height参数,该参数是在ggalluvial版本0.9.2中引入的,如here所示。

xesrikrc

xesrikrc2#

非常有帮助,谢谢你的帖子!我在@Arienrhod回答中找到了一个解决办法(对不起,由于声誉不高,我不能发表评论)。您可以创建一个与数据相同长度的因子,并在geom_stratum中以适当的顺序分配单个类别(aes(fill ='your. factor'),width = 4/12,color =“grey 36”),然后使用“scale_fill_manual()”,如上所示。这是一个苦差事,但它的工作。

相关问题