sankey flow diagram: links going one axis to another has multiple links










0















I am using generic Diabetes data,



datGluBMIAge <- dat[, .(freq = sum(freq)), by=list(Glucose_cat, BMI_cat, Age_cat, Outcome_cat)]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Age_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Glucose_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$BMI_cat))]
setnames(datGluBMIAge, old = c('Glucose_cat', 'Age_cat','Outcome_cat', 'BMI_cat'), new = c('Glucose', 'Age','Diabetes','BMI'))

ggplot(datGluBMIAge,aes(axis1= Diabetes, axis2=Glucose, axis3 = BMI, axis4 = Age, y = freq)) +
geom_alluvium(aes(fill=Diabetes),aes.bind=TRUE, reverse = FALSE, alpha=0.9) +
scale_fill_manual(labels = c("Negative", "Positive"), values = c("#0066BA", "#FF9400")) +
scale_x_discrete(limits = c("Diabetes", "Glucose","BMI", "Age"), expand = c(0, 0)) +
scale_y_continuous(labels = NULL, expand = c(0,0))+
theme(axis.text.x=element_text(margin = margin(t = 0, unit='pt')),
axis.title.x = element_text(vjust = 0),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
legend.position = "none")+
geom_stratum(alpha=1, reverse = FALSE) +
geom_text(stat="stratum", label.strata= TRUE, reverse = FALSE) +
ylab(NULL)+xlab(NULL) +
geom_vline(xintercept = 0)


Above code produces this plot:





I have two question based on the above plot



  1. there are various links going from Glucose="Normal" to BMI='30-35', how do i arrange them such a way that i see only one link going from Glucose="Normal" to BMI='30-35' and all the others


  2. How can I change the color scheme between axis?for example if I want different colors between Glucose and BMI,and even different color between BMI and Age? How can I do it using the ggalluvial library?


any leads would be appreciated. Thank you for your time. Regards,Trupti










share|improve this question
























  • @MrFlick could you please take time to address my concern? I see it very similar to link

    – Trupti
    Oct 26 '18 at 9:28















0















I am using generic Diabetes data,



datGluBMIAge <- dat[, .(freq = sum(freq)), by=list(Glucose_cat, BMI_cat, Age_cat, Outcome_cat)]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Age_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Glucose_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$BMI_cat))]
setnames(datGluBMIAge, old = c('Glucose_cat', 'Age_cat','Outcome_cat', 'BMI_cat'), new = c('Glucose', 'Age','Diabetes','BMI'))

ggplot(datGluBMIAge,aes(axis1= Diabetes, axis2=Glucose, axis3 = BMI, axis4 = Age, y = freq)) +
geom_alluvium(aes(fill=Diabetes),aes.bind=TRUE, reverse = FALSE, alpha=0.9) +
scale_fill_manual(labels = c("Negative", "Positive"), values = c("#0066BA", "#FF9400")) +
scale_x_discrete(limits = c("Diabetes", "Glucose","BMI", "Age"), expand = c(0, 0)) +
scale_y_continuous(labels = NULL, expand = c(0,0))+
theme(axis.text.x=element_text(margin = margin(t = 0, unit='pt')),
axis.title.x = element_text(vjust = 0),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
legend.position = "none")+
geom_stratum(alpha=1, reverse = FALSE) +
geom_text(stat="stratum", label.strata= TRUE, reverse = FALSE) +
ylab(NULL)+xlab(NULL) +
geom_vline(xintercept = 0)


Above code produces this plot:





I have two question based on the above plot



  1. there are various links going from Glucose="Normal" to BMI='30-35', how do i arrange them such a way that i see only one link going from Glucose="Normal" to BMI='30-35' and all the others


  2. How can I change the color scheme between axis?for example if I want different colors between Glucose and BMI,and even different color between BMI and Age? How can I do it using the ggalluvial library?


any leads would be appreciated. Thank you for your time. Regards,Trupti










share|improve this question
























  • @MrFlick could you please take time to address my concern? I see it very similar to link

    – Trupti
    Oct 26 '18 at 9:28













0












0








0








I am using generic Diabetes data,



datGluBMIAge <- dat[, .(freq = sum(freq)), by=list(Glucose_cat, BMI_cat, Age_cat, Outcome_cat)]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Age_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Glucose_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$BMI_cat))]
setnames(datGluBMIAge, old = c('Glucose_cat', 'Age_cat','Outcome_cat', 'BMI_cat'), new = c('Glucose', 'Age','Diabetes','BMI'))

ggplot(datGluBMIAge,aes(axis1= Diabetes, axis2=Glucose, axis3 = BMI, axis4 = Age, y = freq)) +
geom_alluvium(aes(fill=Diabetes),aes.bind=TRUE, reverse = FALSE, alpha=0.9) +
scale_fill_manual(labels = c("Negative", "Positive"), values = c("#0066BA", "#FF9400")) +
scale_x_discrete(limits = c("Diabetes", "Glucose","BMI", "Age"), expand = c(0, 0)) +
scale_y_continuous(labels = NULL, expand = c(0,0))+
theme(axis.text.x=element_text(margin = margin(t = 0, unit='pt')),
axis.title.x = element_text(vjust = 0),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
legend.position = "none")+
geom_stratum(alpha=1, reverse = FALSE) +
geom_text(stat="stratum", label.strata= TRUE, reverse = FALSE) +
ylab(NULL)+xlab(NULL) +
geom_vline(xintercept = 0)


Above code produces this plot:





I have two question based on the above plot



  1. there are various links going from Glucose="Normal" to BMI='30-35', how do i arrange them such a way that i see only one link going from Glucose="Normal" to BMI='30-35' and all the others


  2. How can I change the color scheme between axis?for example if I want different colors between Glucose and BMI,and even different color between BMI and Age? How can I do it using the ggalluvial library?


any leads would be appreciated. Thank you for your time. Regards,Trupti










share|improve this question
















I am using generic Diabetes data,



datGluBMIAge <- dat[, .(freq = sum(freq)), by=list(Glucose_cat, BMI_cat, Age_cat, Outcome_cat)]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Age_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$Glucose_cat))]
datGluBMIAge<- datGluBMIAge[!(is.na(datGluBMIAge$BMI_cat))]
setnames(datGluBMIAge, old = c('Glucose_cat', 'Age_cat','Outcome_cat', 'BMI_cat'), new = c('Glucose', 'Age','Diabetes','BMI'))

ggplot(datGluBMIAge,aes(axis1= Diabetes, axis2=Glucose, axis3 = BMI, axis4 = Age, y = freq)) +
geom_alluvium(aes(fill=Diabetes),aes.bind=TRUE, reverse = FALSE, alpha=0.9) +
scale_fill_manual(labels = c("Negative", "Positive"), values = c("#0066BA", "#FF9400")) +
scale_x_discrete(limits = c("Diabetes", "Glucose","BMI", "Age"), expand = c(0, 0)) +
scale_y_continuous(labels = NULL, expand = c(0,0))+
theme(axis.text.x=element_text(margin = margin(t = 0, unit='pt')),
axis.title.x = element_text(vjust = 0),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
legend.position = "none")+
geom_stratum(alpha=1, reverse = FALSE) +
geom_text(stat="stratum", label.strata= TRUE, reverse = FALSE) +
ylab(NULL)+xlab(NULL) +
geom_vline(xintercept = 0)


Above code produces this plot:





I have two question based on the above plot



  1. there are various links going from Glucose="Normal" to BMI='30-35', how do i arrange them such a way that i see only one link going from Glucose="Normal" to BMI='30-35' and all the others


  2. How can I change the color scheme between axis?for example if I want different colors between Glucose and BMI,and even different color between BMI and Age? How can I do it using the ggalluvial library?


any leads would be appreciated. Thank you for your time. Regards,Trupti







r sankey-diagram






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 25 '18 at 14:45









Jaap

57k21122136




57k21122136










asked Oct 25 '18 at 13:32









TruptiTrupti

134




134












  • @MrFlick could you please take time to address my concern? I see it very similar to link

    – Trupti
    Oct 26 '18 at 9:28

















  • @MrFlick could you please take time to address my concern? I see it very similar to link

    – Trupti
    Oct 26 '18 at 9:28
















@MrFlick could you please take time to address my concern? I see it very similar to link

– Trupti
Oct 26 '18 at 9:28





@MrFlick could you please take time to address my concern? I see it very similar to link

– Trupti
Oct 26 '18 at 9:28












2 Answers
2






active

oldest

votes


















0














Since the strata are unique to each axis, you can change the color scheme for the flows between each pair of axes by first converting to long form and then using geom_flow(aes(fill = stratum)). Here's an example derived from the cover illustration to ggalluvial:



ggplot(data = titanic_long,
aes(x = Demographic, stratum = stratum, alluvium = alluvium,
y = Freq, label = stratum)) +
geom_flow(aes(fill = stratum)) +
geom_stratum() + geom_text(stat = "stratum") +
theme_minimal() +
ggtitle("passengers on the maiden voyage of the Titanic",
"stratified by demographics and survival")


enter image description here



If, on the other hand, you have some strata that appear at multiple axes, you'll need to take the additional step of distinguishing these as separate factor levels. You can do this manually after converting to long form, or by setting discern = TRUE in to_lodes_form() when converting to long form.






share|improve this answer
































    0














    Instead of using geom_alluvium using geom_flow solves the question 1. I am still exploring to resolve question 2. Thanks, Trupti






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52990586%2fsankey-flow-diagram-links-going-one-axis-to-another-has-multiple-links%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Since the strata are unique to each axis, you can change the color scheme for the flows between each pair of axes by first converting to long form and then using geom_flow(aes(fill = stratum)). Here's an example derived from the cover illustration to ggalluvial:



      ggplot(data = titanic_long,
      aes(x = Demographic, stratum = stratum, alluvium = alluvium,
      y = Freq, label = stratum)) +
      geom_flow(aes(fill = stratum)) +
      geom_stratum() + geom_text(stat = "stratum") +
      theme_minimal() +
      ggtitle("passengers on the maiden voyage of the Titanic",
      "stratified by demographics and survival")


      enter image description here



      If, on the other hand, you have some strata that appear at multiple axes, you'll need to take the additional step of distinguishing these as separate factor levels. You can do this manually after converting to long form, or by setting discern = TRUE in to_lodes_form() when converting to long form.






      share|improve this answer





























        0














        Since the strata are unique to each axis, you can change the color scheme for the flows between each pair of axes by first converting to long form and then using geom_flow(aes(fill = stratum)). Here's an example derived from the cover illustration to ggalluvial:



        ggplot(data = titanic_long,
        aes(x = Demographic, stratum = stratum, alluvium = alluvium,
        y = Freq, label = stratum)) +
        geom_flow(aes(fill = stratum)) +
        geom_stratum() + geom_text(stat = "stratum") +
        theme_minimal() +
        ggtitle("passengers on the maiden voyage of the Titanic",
        "stratified by demographics and survival")


        enter image description here



        If, on the other hand, you have some strata that appear at multiple axes, you'll need to take the additional step of distinguishing these as separate factor levels. You can do this manually after converting to long form, or by setting discern = TRUE in to_lodes_form() when converting to long form.






        share|improve this answer



























          0












          0








          0







          Since the strata are unique to each axis, you can change the color scheme for the flows between each pair of axes by first converting to long form and then using geom_flow(aes(fill = stratum)). Here's an example derived from the cover illustration to ggalluvial:



          ggplot(data = titanic_long,
          aes(x = Demographic, stratum = stratum, alluvium = alluvium,
          y = Freq, label = stratum)) +
          geom_flow(aes(fill = stratum)) +
          geom_stratum() + geom_text(stat = "stratum") +
          theme_minimal() +
          ggtitle("passengers on the maiden voyage of the Titanic",
          "stratified by demographics and survival")


          enter image description here



          If, on the other hand, you have some strata that appear at multiple axes, you'll need to take the additional step of distinguishing these as separate factor levels. You can do this manually after converting to long form, or by setting discern = TRUE in to_lodes_form() when converting to long form.






          share|improve this answer















          Since the strata are unique to each axis, you can change the color scheme for the flows between each pair of axes by first converting to long form and then using geom_flow(aes(fill = stratum)). Here's an example derived from the cover illustration to ggalluvial:



          ggplot(data = titanic_long,
          aes(x = Demographic, stratum = stratum, alluvium = alluvium,
          y = Freq, label = stratum)) +
          geom_flow(aes(fill = stratum)) +
          geom_stratum() + geom_text(stat = "stratum") +
          theme_minimal() +
          ggtitle("passengers on the maiden voyage of the Titanic",
          "stratified by demographics and survival")


          enter image description here



          If, on the other hand, you have some strata that appear at multiple axes, you'll need to take the additional step of distinguishing these as separate factor levels. You can do this manually after converting to long form, or by setting discern = TRUE in to_lodes_form() when converting to long form.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 15:41

























          answered Nov 14 '18 at 15:20









          Cory BrunsonCory Brunson

          13616




          13616























              0














              Instead of using geom_alluvium using geom_flow solves the question 1. I am still exploring to resolve question 2. Thanks, Trupti






              share|improve this answer



























                0














                Instead of using geom_alluvium using geom_flow solves the question 1. I am still exploring to resolve question 2. Thanks, Trupti






                share|improve this answer

























                  0












                  0








                  0







                  Instead of using geom_alluvium using geom_flow solves the question 1. I am still exploring to resolve question 2. Thanks, Trupti






                  share|improve this answer













                  Instead of using geom_alluvium using geom_flow solves the question 1. I am still exploring to resolve question 2. Thanks, Trupti







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 26 '18 at 11:26









                  TruptiTrupti

                  134




                  134



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52990586%2fsankey-flow-diagram-links-going-one-axis-to-another-has-multiple-links%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo