[英]Alluvial Charts - Flow is not showing
I have a table similar to this one:我有一张类似于这张的桌子:
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
I would like to build an alluvial chart that could highlight the different topics covered by each organization during the fortnight.我想建立一个冲积图表,可以突出每个组织在两周内涵盖的不同主题。 I managed to create a chart like this one but the flow isn't work.我设法创建了一个这样的图表,但流程不起作用。
So far, what I have done was it:到目前为止,我所做的是:
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")
Could you help me to identify why the alluvial chart is not working properly?你能帮我确定为什么冲积图不能正常工作吗?
It is due to incorrect usage of aes
in ggplot
.这是由于ggplot
中的aes
使用不正确。 The following code以下代码
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)
gives this output:给出这个 output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.