Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it true that one node can connect with one next-node by only one flow? (how to merge two plots?) #44

Open
christina-cqq opened this issue Nov 1, 2023 · 0 comments

Comments

@christina-cqq
Copy link

christina-cqq commented Nov 1, 2023

I wanted to merge two sankey plots (mariculture and capture) with the same nodes (oceans) and the same next-node (country's income level), but different flow values (mariculture amount and captured amount). I was planning to use one sankey plot to show the two flow values at one time, the total values at nodes and next-nodes can be respectively added up. But it seems to be unable to do so? hence the question, is it true that one node can connect with one next-node by only one flow? In my case, I want to connect them with two flows.

library(ggplot2)
library(ggsankey)
library(dplyr)
#prepare the mariculture data, node is ocean, next_node is income level, value is seafood amount   
 dfa<-dfaqua.incomelevel %>% 
  select (node , next_node , value)%>% 
  make_long(node, next_node, value = value)  %>% 
  mutate(type = 'Mariculture') 
dfa<- data.frame(dfa)

#prepare the capture data, node is ocean, next_node is income level, value is seafood amount 
dfc<-dfcatch.incomelevel %>% 
  select (node , next_node , value)%>% 
  make_long(node, next_node, value = value)   %>% 
  mutate(type = 'Capture') 
dfc<- data.frame(dfc)

#combine the two make_long data, data are provided here https://filetransfer.io/data-package/5wKjjvvU#link
dfinput <- rbind(dfa, dfc)
dfinput <-dfinput%>%
    group_by(x, node) %>%
    mutate(total = sum(value))

#make the merged sankey plot, but the output is weird, not what I want
g  <- ggplot (dfinput
                    , aes(x = x
                    , next_x = next_x
                    , node = node
                    , next_node = next_node
                    , fill = factor(type) #only colour the flow by type: mariculture and capture
                    , value = value #flow value
                    , label = paste0(node, " (", round(total,0), ")")
))+
  geom_sankey(flow.alpha = 0.5, node.color = 1) +
  geom_sankey_label(size = 3.5, color = 1, fill = "white") +
  scale_fill_viridis_d() +
  theme_sankey(base_size = 16)+
  guides(fill = guide_legend(title = "Income Level"))
print(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant