【问题标题】:Alluvial Charts - Flow is not showing冲积图 - 流量未显示
【发布时间】:2020-07-17 00:14:42
【问题描述】:

我有一张和这张类似的桌子:

Organization    Timeframe   Code                    id
1   Agencia1    Fortnight 1 International Affairs   1
2   Agencia2    Fortnight 1 Environment             2
3   Agencia2    Fortnight 1 Health                  4
4   Agencia2    Fortnight 1 Public Policy           5
5   Agencia1    Fortnight 2 Politics                6
6   Agencia2    Fortnight 2 Disaster                7
7   Agencia1    Fortnight 2 Public Policy           8
8   Agencia1    Fortnight 2 Federal Government      9
9   Agencia1    Fortnight 2 Business                10
10  Agencia1    Fortnight 3 Federal Government      11
11  Agencia2    Fortnight 3 Dissemination - COVID19 12
12  Agencia1    Fortnight 3 Transparency - COVID19  13
13  Agencia2    Fortnight 3 Economy - COVID19       14
14  Agencia1    Fortnight 3 Prevention - COVID19    15
15  Agencia1    Fortnight 4 Economy                 16
16  Agencia1    Fortnight 4 Media                   17
17  Agencia1    Fortnight 4 Leisure                 18
18  Agencia1    Fortnight 4 Politics                19
19  Agencia1    Fortnight 4 Prevention - COVID19    20
20  Agencia1    Fortnight 5 Prevention - COVID19    21

我想制作一个冲积图,可以突出显示每个组织在两周内涵盖的不同主题。我设法创建了一个这样的图表,但流程不起作用。

到目前为止,我所做的是:

alluvial_data <- as.data.frame(FC_Outlets %>%select(Organization, Timeframe, Code))
alluvial_data <- alluvial_data %>% mutate(id = row_number())

#Remove duplicates
alluvial_data <- alluvial_data %>% 
  distinct(Organization, Timeframe, Code, .keep_all = TRUE)


# Convert Timeframe to Factor - Categorical Variable
alluvial_data$Timeframe <-as.factor(alluvial_data$Timeframe)

# Convert Code to String
alluvial_data$Code <-as.character(alluvial_data$Code)

library(RColorBrewer)

# Define the number of colors you want
nb.cols <- 10
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)


# Chart
ggplot(alluvial_data,
       aes(x = Timeframe, stratum = Code, alluvium = id,
           fill = Code, label = Code)) +
  #scale_fill_brewer(type = "qual", palette = "Set2") +
  scale_fill_manual(values = mycolors) +
  geom_flow(stat = "alluvium", lode.guidance = "frontback",
            color = "darkgray") +
  geom_stratum() +
  theme(legend.position = "bottom") +
  ggtitle("Organizations")

你能帮我找出为什么冲积图不能正常工作吗?

【问题讨论】:

    标签: r ggplot2 charts ggplotly


    【解决方案1】:

    这是由于在ggplot 中错误使用了aes。以下代码

    c <- c(LETTERS[1:4], LETTERS[2:6], LETTERS[3:7], LETTERS[3:8])
    t <- c(rep("Fortnight 1",4), rep("Fortnight 2",5), rep("Fortnight 3",5), rep("Fortnight 4",6))
    s <- c(rep(c("Female","Male"),10))
    ag <- c(2,3,4,6,11,13)
    f <- rnorm(20,20,99)
    df <- data.frame(Timeframe=t,Code=c,Sex=s,Freq=round(abs(f))) %>% mutate(Organization=ifelse((row_number() %in% ag), "Agencia2","Agencia1" ))
    
    alluvial_data <- as.data.frame(df %>%select(Organization, Timeframe, Code, Freq, Sex))
    alluvial_data <- alluvial_data %>% mutate(id = row_number())
    
    #Remove duplicates
    alluvial_data <- alluvial_data %>% 
      distinct(Organization, Timeframe, Code, Sex, .keep_all = TRUE)
    
    #levels(alluvial_data$Timeframe)
    
    # Convert Timeframe to Factor - Categorical Variable
    alluvial_data$Timeframe <-as.factor(alluvial_data$Timeframe)
    
    # Convert Code to String
    alluvial_data$Code <-as.character(alluvial_data$Code)
    
    library(RColorBrewer)
    
    # Define the number of colors you want
    nb.cols <- 10
    mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
    mycolor2 <- colorRampPalette(brewer.pal(2, "Set2"))(nb.cols)
    
    # Chart
    ggplot(alluvial_data,
           aes(y = Freq, axis1 = Organization, axis2 = Timeframe, axis3 = Code,fill=Sex)) +
      #scale_fill_brewer(type = "qual", palette = "Set2") +
      scale_x_discrete(limits=c("Organization","Timeframe","Code"), expand=c(0.05,0.05)) +
      scale_fill_manual(values = mycolors) +
      geom_flow(stat = "alluvium", lode.guidance = "frontback" #, color="grey"
                 ) +
      geom_stratum(width = 1/4, fill = "cyan", color = "grey") +
      geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
      theme(legend.position = "bottom") +
      ggtitle("Organizations") +
      guides(fill=guide_legend(override.aes = list(color=mycolors[1:2])))+
      labs(fill=NULL)
    

    给出这个输出:

    【讨论】:

    • 嗨@YBS,感谢您回答我。但不是在轴组织、时间框架和代码中,我想在第一张图片中拥有时间框架:第 1 周、第 2 周、第 3 周、第 4 周和第 5 周(甚至更多)。而且,我的想法是建立组织,而不是男性和女性。有可能吗?
    • 是的,请使用您的数据根据​​您的需要更改轴。我的数据只是虚拟数据。请注意,在这个数据结构中,Timeframe 只能是一个轴。如果您的数据仍然是冲积形式,那么您可以做到。您可以通过is_alluvia_form() 查询
    猜你喜欢
    • 2022-12-19
    • 1970-01-01
    • 2022-01-12
    • 2022-01-04
    • 1970-01-01
    • 2021-03-19
    • 2020-12-10
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多