ggalluvial package in R - Setting alluvial fill color axis by axis (rather than solely by final axis)creating a more continuous color palette in r, ggplot2, lattice, or latticeExtraConsistent legend colors with qplotAdding transparent circles of defined radius to existing plot in RMultiple filled.countour plots in one graphhow do I match color palettes in multiple ggplot2 graphs in R?Different colored values by facet in ggplot2 geom_pointrange with significance annotationggplot2 Vertical Bars Nested in Horizontal Bars [product plot]set factor levels in ggplot with changing values based on slider bar in r markdownggplot2: Fix colors to factor levelsSetting a fixed color scale for a series of data in ggplot2

How to slice a string input at a certain unknown index

Can the word "desk" be used as a verb?

Is it okay to use open source code to do an interview task?

Is there a formal/better word than "skyrocket" for the given context?

Can a landlord force all residents to use the landlord's in-house debit card accounts?

What term do you use for someone who acts impulsively?

How to "add vert" in blender 2.8?

Is it possible for a character at any level to cast all 44 Cantrips in one week without Magic Items?

Can Jimmy hang on his rope?

Why won't the U.S. sign a peace treaty with North Korea?

Who goes first? Person disembarking bus or the bicycle?

Four ships at the ocean with the same distance

Intern not wearing safety equipment; how could I have handled this differently?

How do I talk to my wife about unrealistic expectations?

How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant

Why did Robert F. Kennedy loathe Lyndon B. Johnson?

Is it possible to complete a PhD in CS in 3 years?

Is homosexuality or bisexuality allowed for women?

How to use Adostop Eco stop bath?

QR codes, do people use them?

Why do airports remove/realign runways?

Matrices with shadows

Would denouncing cheaters from an exam make me less likely to receive penalties?

Find out what encryptor name my database is using



ggalluvial package in R - Setting alluvial fill color axis by axis (rather than solely by final axis)


creating a more continuous color palette in r, ggplot2, lattice, or latticeExtraConsistent legend colors with qplotAdding transparent circles of defined radius to existing plot in RMultiple filled.countour plots in one graphhow do I match color palettes in multiple ggplot2 graphs in R?Different colored values by facet in ggplot2 geom_pointrange with significance annotationggplot2 Vertical Bars Nested in Horizontal Bars [product plot]set factor levels in ggplot with changing values based on slider bar in r markdownggplot2: Fix colors to factor levelsSetting a fixed color scale for a series of data in ggplot2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am currently trying to produce an alluvial plot in R with the ggalluvial package. With it I wish to plot the successive migration across multiple between different values across successive segments of time (Seg1, Seg2, Seg3, Seg4). At Seg 1 all cases have the value of "workseg"; at Seg 2 the value may be one of three other values (related content, unrelated content, NONE); Seg3 and Seg4 values can be any of the four options.



Using the following code...



##Reorder levels per segment (make vertical order of strata levels identical 
across all axes, rather than "zig-zag" --> this is just an aesthetic
preference)##

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))


##Plot##

ggplot(as.data.frame(dRG.lode),
aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) +
geom_alluvium(aes(fill = Seg4), width = 1/12) +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")


...I have been able to produce the following plot:



...enter image description here



My main question:



1) Is there a means by which to have the alluvial fill color not be consistent across an entire alluvial strand from start to finish, based on the Seg4 value, but INSTEAD to have the color change axis by axis, based upon the current axis value? For instance, I'd like all strands with a strata value of "workseg" at a given axis to be blue between that axis and the previous axis. Something akin to this seems possible based upon the vaccinations example at the bottom of this vignette (see last plot above appendix). The fills in that example reflect the strata they each came from, axis to axis (e.g., all strands coming from a "Never" strata are teal, regardless of their value at the next axis). I basically want to implement the inverse of this - that is, fill based on the strata of the next axis (e.g., all strands leading to a "workseg" strata are blue, regardless of where their value at the previous axis).



An unrelated, secondary question:



2) Is there any means by which to add annotation to alluvia? That is, they axes contain strata labels based upon values in the data set, but is there a means by which to add labels or other annotative information to the strands themselves (beyond manually in post-production)?










share|improve this question
























  • It's good that you can recognize that there are 2 separate questions at play. It's easier to help if you make them actually 2 questions; you can split the part about labels into another reproducible post

    – camille
    Mar 26 at 2:31






  • 1





    Can you provide a MCVE for this problem? With reproducible data & preferably fewer axes / strata that reflect what you are trying to do?

    – Z.Lin
    Mar 26 at 2:33











  • @camille Thanks; I'll create a second post for question 2 with updated descriptions and code sample once question 1 is resolved (to reduce chance of have two independently working but irreconcilable solutions :) ).

    – jjcii
    Mar 26 at 14:51











  • @Z.Lin Thanks. The data can be found here. As number of axes/strata, this is already a minimal set, though I can provide a MCVE with fewer arguments, streamlining consideration (next comment).

    – jjcii
    Mar 26 at 15:05











  • ggplot(as.data.frame(mydata), aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) + geom_alluvium(aes(fill = Seg4)) + geom_label(stat = "stratum", label.strata = TRUE) + scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4")) Adjusting the fill value changes the alluvial coloring. I'm not sure if this argument alone can accomplish my goal or if additional arguments need to be considered (see code for vaccination example linked in original post).

    – jjcii
    Mar 26 at 15:05


















0















I am currently trying to produce an alluvial plot in R with the ggalluvial package. With it I wish to plot the successive migration across multiple between different values across successive segments of time (Seg1, Seg2, Seg3, Seg4). At Seg 1 all cases have the value of "workseg"; at Seg 2 the value may be one of three other values (related content, unrelated content, NONE); Seg3 and Seg4 values can be any of the four options.



Using the following code...



##Reorder levels per segment (make vertical order of strata levels identical 
across all axes, rather than "zig-zag" --> this is just an aesthetic
preference)##

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))


##Plot##

ggplot(as.data.frame(dRG.lode),
aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) +
geom_alluvium(aes(fill = Seg4), width = 1/12) +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")


...I have been able to produce the following plot:



...enter image description here



My main question:



1) Is there a means by which to have the alluvial fill color not be consistent across an entire alluvial strand from start to finish, based on the Seg4 value, but INSTEAD to have the color change axis by axis, based upon the current axis value? For instance, I'd like all strands with a strata value of "workseg" at a given axis to be blue between that axis and the previous axis. Something akin to this seems possible based upon the vaccinations example at the bottom of this vignette (see last plot above appendix). The fills in that example reflect the strata they each came from, axis to axis (e.g., all strands coming from a "Never" strata are teal, regardless of their value at the next axis). I basically want to implement the inverse of this - that is, fill based on the strata of the next axis (e.g., all strands leading to a "workseg" strata are blue, regardless of where their value at the previous axis).



An unrelated, secondary question:



2) Is there any means by which to add annotation to alluvia? That is, they axes contain strata labels based upon values in the data set, but is there a means by which to add labels or other annotative information to the strands themselves (beyond manually in post-production)?










share|improve this question
























  • It's good that you can recognize that there are 2 separate questions at play. It's easier to help if you make them actually 2 questions; you can split the part about labels into another reproducible post

    – camille
    Mar 26 at 2:31






  • 1





    Can you provide a MCVE for this problem? With reproducible data & preferably fewer axes / strata that reflect what you are trying to do?

    – Z.Lin
    Mar 26 at 2:33











  • @camille Thanks; I'll create a second post for question 2 with updated descriptions and code sample once question 1 is resolved (to reduce chance of have two independently working but irreconcilable solutions :) ).

    – jjcii
    Mar 26 at 14:51











  • @Z.Lin Thanks. The data can be found here. As number of axes/strata, this is already a minimal set, though I can provide a MCVE with fewer arguments, streamlining consideration (next comment).

    – jjcii
    Mar 26 at 15:05











  • ggplot(as.data.frame(mydata), aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) + geom_alluvium(aes(fill = Seg4)) + geom_label(stat = "stratum", label.strata = TRUE) + scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4")) Adjusting the fill value changes the alluvial coloring. I'm not sure if this argument alone can accomplish my goal or if additional arguments need to be considered (see code for vaccination example linked in original post).

    – jjcii
    Mar 26 at 15:05














0












0








0








I am currently trying to produce an alluvial plot in R with the ggalluvial package. With it I wish to plot the successive migration across multiple between different values across successive segments of time (Seg1, Seg2, Seg3, Seg4). At Seg 1 all cases have the value of "workseg"; at Seg 2 the value may be one of three other values (related content, unrelated content, NONE); Seg3 and Seg4 values can be any of the four options.



Using the following code...



##Reorder levels per segment (make vertical order of strata levels identical 
across all axes, rather than "zig-zag" --> this is just an aesthetic
preference)##

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))


##Plot##

ggplot(as.data.frame(dRG.lode),
aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) +
geom_alluvium(aes(fill = Seg4), width = 1/12) +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")


...I have been able to produce the following plot:



...enter image description here



My main question:



1) Is there a means by which to have the alluvial fill color not be consistent across an entire alluvial strand from start to finish, based on the Seg4 value, but INSTEAD to have the color change axis by axis, based upon the current axis value? For instance, I'd like all strands with a strata value of "workseg" at a given axis to be blue between that axis and the previous axis. Something akin to this seems possible based upon the vaccinations example at the bottom of this vignette (see last plot above appendix). The fills in that example reflect the strata they each came from, axis to axis (e.g., all strands coming from a "Never" strata are teal, regardless of their value at the next axis). I basically want to implement the inverse of this - that is, fill based on the strata of the next axis (e.g., all strands leading to a "workseg" strata are blue, regardless of where their value at the previous axis).



An unrelated, secondary question:



2) Is there any means by which to add annotation to alluvia? That is, they axes contain strata labels based upon values in the data set, but is there a means by which to add labels or other annotative information to the strands themselves (beyond manually in post-production)?










share|improve this question
















I am currently trying to produce an alluvial plot in R with the ggalluvial package. With it I wish to plot the successive migration across multiple between different values across successive segments of time (Seg1, Seg2, Seg3, Seg4). At Seg 1 all cases have the value of "workseg"; at Seg 2 the value may be one of three other values (related content, unrelated content, NONE); Seg3 and Seg4 values can be any of the four options.



Using the following code...



##Reorder levels per segment (make vertical order of strata levels identical 
across all axes, rather than "zig-zag" --> this is just an aesthetic
preference)##

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))


##Plot##

ggplot(as.data.frame(dRG.lode),
aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) +
geom_alluvium(aes(fill = Seg4), width = 1/12) +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")


...I have been able to produce the following plot:



...enter image description here



My main question:



1) Is there a means by which to have the alluvial fill color not be consistent across an entire alluvial strand from start to finish, based on the Seg4 value, but INSTEAD to have the color change axis by axis, based upon the current axis value? For instance, I'd like all strands with a strata value of "workseg" at a given axis to be blue between that axis and the previous axis. Something akin to this seems possible based upon the vaccinations example at the bottom of this vignette (see last plot above appendix). The fills in that example reflect the strata they each came from, axis to axis (e.g., all strands coming from a "Never" strata are teal, regardless of their value at the next axis). I basically want to implement the inverse of this - that is, fill based on the strata of the next axis (e.g., all strands leading to a "workseg" strata are blue, regardless of where their value at the previous axis).



An unrelated, secondary question:



2) Is there any means by which to add annotation to alluvia? That is, they axes contain strata labels based upon values in the data set, but is there a means by which to add labels or other annotative information to the strands themselves (beyond manually in post-production)?







r ggplot2 plot data-visualization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 15:18







jjcii

















asked Mar 25 at 22:34









jjciijjcii

321 silver badge7 bronze badges




321 silver badge7 bronze badges












  • It's good that you can recognize that there are 2 separate questions at play. It's easier to help if you make them actually 2 questions; you can split the part about labels into another reproducible post

    – camille
    Mar 26 at 2:31






  • 1





    Can you provide a MCVE for this problem? With reproducible data & preferably fewer axes / strata that reflect what you are trying to do?

    – Z.Lin
    Mar 26 at 2:33











  • @camille Thanks; I'll create a second post for question 2 with updated descriptions and code sample once question 1 is resolved (to reduce chance of have two independently working but irreconcilable solutions :) ).

    – jjcii
    Mar 26 at 14:51











  • @Z.Lin Thanks. The data can be found here. As number of axes/strata, this is already a minimal set, though I can provide a MCVE with fewer arguments, streamlining consideration (next comment).

    – jjcii
    Mar 26 at 15:05











  • ggplot(as.data.frame(mydata), aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) + geom_alluvium(aes(fill = Seg4)) + geom_label(stat = "stratum", label.strata = TRUE) + scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4")) Adjusting the fill value changes the alluvial coloring. I'm not sure if this argument alone can accomplish my goal or if additional arguments need to be considered (see code for vaccination example linked in original post).

    – jjcii
    Mar 26 at 15:05


















  • It's good that you can recognize that there are 2 separate questions at play. It's easier to help if you make them actually 2 questions; you can split the part about labels into another reproducible post

    – camille
    Mar 26 at 2:31






  • 1





    Can you provide a MCVE for this problem? With reproducible data & preferably fewer axes / strata that reflect what you are trying to do?

    – Z.Lin
    Mar 26 at 2:33











  • @camille Thanks; I'll create a second post for question 2 with updated descriptions and code sample once question 1 is resolved (to reduce chance of have two independently working but irreconcilable solutions :) ).

    – jjcii
    Mar 26 at 14:51











  • @Z.Lin Thanks. The data can be found here. As number of axes/strata, this is already a minimal set, though I can provide a MCVE with fewer arguments, streamlining consideration (next comment).

    – jjcii
    Mar 26 at 15:05











  • ggplot(as.data.frame(mydata), aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) + geom_alluvium(aes(fill = Seg4)) + geom_label(stat = "stratum", label.strata = TRUE) + scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4")) Adjusting the fill value changes the alluvial coloring. I'm not sure if this argument alone can accomplish my goal or if additional arguments need to be considered (see code for vaccination example linked in original post).

    – jjcii
    Mar 26 at 15:05

















It's good that you can recognize that there are 2 separate questions at play. It's easier to help if you make them actually 2 questions; you can split the part about labels into another reproducible post

– camille
Mar 26 at 2:31





It's good that you can recognize that there are 2 separate questions at play. It's easier to help if you make them actually 2 questions; you can split the part about labels into another reproducible post

– camille
Mar 26 at 2:31




1




1





Can you provide a MCVE for this problem? With reproducible data & preferably fewer axes / strata that reflect what you are trying to do?

– Z.Lin
Mar 26 at 2:33





Can you provide a MCVE for this problem? With reproducible data & preferably fewer axes / strata that reflect what you are trying to do?

– Z.Lin
Mar 26 at 2:33













@camille Thanks; I'll create a second post for question 2 with updated descriptions and code sample once question 1 is resolved (to reduce chance of have two independently working but irreconcilable solutions :) ).

– jjcii
Mar 26 at 14:51





@camille Thanks; I'll create a second post for question 2 with updated descriptions and code sample once question 1 is resolved (to reduce chance of have two independently working but irreconcilable solutions :) ).

– jjcii
Mar 26 at 14:51













@Z.Lin Thanks. The data can be found here. As number of axes/strata, this is already a minimal set, though I can provide a MCVE with fewer arguments, streamlining consideration (next comment).

– jjcii
Mar 26 at 15:05





@Z.Lin Thanks. The data can be found here. As number of axes/strata, this is already a minimal set, though I can provide a MCVE with fewer arguments, streamlining consideration (next comment).

– jjcii
Mar 26 at 15:05













ggplot(as.data.frame(mydata), aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) + geom_alluvium(aes(fill = Seg4)) + geom_label(stat = "stratum", label.strata = TRUE) + scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4")) Adjusting the fill value changes the alluvial coloring. I'm not sure if this argument alone can accomplish my goal or if additional arguments need to be considered (see code for vaccination example linked in original post).

– jjcii
Mar 26 at 15:05






ggplot(as.data.frame(mydata), aes(axis1 = Seg1, axis2 = Seg2, axis3 = Seg3, axis4 = Seg4)) + geom_alluvium(aes(fill = Seg4)) + geom_label(stat = "stratum", label.strata = TRUE) + scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4")) Adjusting the fill value changes the alluvial coloring. I'm not sure if this argument alone can accomplish my goal or if additional arguments need to be considered (see code for vaccination example linked in original post).

– jjcii
Mar 26 at 15:05













1 Answer
1






active

oldest

votes


















1














In the vaccinations example, flows adopt aesthetics from the strata to their lefts via geom_flow() with fill = response (the stratum variable); this can't be done using geom_alluvium(), which renders each complete alluvium as a single graphical object ("grob"). The data you've linked to are in what ggalluvial considers "wide" format, i.e. each axis is a variable, but in order to have a consistent stratum variable the data need to be in "long" format.



The code below makes both of these changes and uses aes.flow = "backward" (see the documentation) to have flows adopt aesthetics from the strata to their rights (instead of their lefts).



library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(ggalluvial)

dRG <- read.csv("~/Downloads/mydata.csv")

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))

dRG.long <- to_lodes_form(dRG.lode, -X,
key = "segment", value = "value", id = "id")

ggplot(dRG.long,
aes(x = segment, stratum = value, alluvium = id)) +
geom_flow(aes(fill = value), width = 1/12, aes.flow = "backward") +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")




Created on 2019-03-28 by the reprex package (v0.2.1)



On reflection, the naming conventions for the parameter aes.flow and its options "forward" and "backward" might not be the most intuitive. I'd welcome suggestions on that!






share|improve this answer























  • This is fantastic, @Cory; many thanks! Some quick follow-ups: 1) The "-X" argument for the dRG.long assignment resulted in the error "object 'X' not found". Removing this argument worked, however. 2) My resulting plot no longer vertically orders the strata per axis as intended (e.g., workseg, related, unrelated, none); could this be related to the above omission? 3) I assume not, but is it possible to set custom colors for each flow value (beyond the pre-set palettes) or for individual axis strata (rather than for the entire axis)? Thanks again.

    – jjcii
    Mar 29 at 16:57












  • @jjcii glad this helps. (1) and (2) sound like version problems; if you update the three packages (dplyr, ggplot2, ggalluvial) and run the code again, do you see the same behavior? (3) should be possible using scale_fill_manual().

    – Cory Brunson
    Mar 29 at 17:30












  • @jjcii i only responded to the first half of (3); sorry. You can use different colors for the same values at different axes by passing discern = TRUE in to_lodes_form(), which i think is what you're after. Note that the documentation calls this parameter but does not use it in the way you have in mind, so you'll have to see for yourself if it does what you want.

    – Cory Brunson
    Mar 29 at 19:10











  • Thanks, @Cory! With that info I was able to tackle (1), (2), and the first part of (3) successfully. As for the discern argument, setting it to TRUE in the to_lodes_form() function results in following when attempting the plot: "Error: Insufficient values in manual scale. 12 needed but only 4 provided." I've set four colors via a pre-set vector, as recommended for the scale_manual guide (thanks!), but evidently the function is asking for a value for each strata of each axis. Any thoughts?

    – jjcii
    Mar 30 at 17:46












  • @jjcii that's actually what i thought you wanted — a different color for each flow, even flows associated with the same value, when they appear at different axes. Is that not correct? If you leave out discern = TRUE, do you get what you want?

    – Cory Brunson
    Mar 30 at 18:00










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%2f55347354%2fggalluvial-package-in-r-setting-alluvial-fill-color-axis-by-axis-rather-than%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In the vaccinations example, flows adopt aesthetics from the strata to their lefts via geom_flow() with fill = response (the stratum variable); this can't be done using geom_alluvium(), which renders each complete alluvium as a single graphical object ("grob"). The data you've linked to are in what ggalluvial considers "wide" format, i.e. each axis is a variable, but in order to have a consistent stratum variable the data need to be in "long" format.



The code below makes both of these changes and uses aes.flow = "backward" (see the documentation) to have flows adopt aesthetics from the strata to their rights (instead of their lefts).



library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(ggalluvial)

dRG <- read.csv("~/Downloads/mydata.csv")

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))

dRG.long <- to_lodes_form(dRG.lode, -X,
key = "segment", value = "value", id = "id")

ggplot(dRG.long,
aes(x = segment, stratum = value, alluvium = id)) +
geom_flow(aes(fill = value), width = 1/12, aes.flow = "backward") +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")




Created on 2019-03-28 by the reprex package (v0.2.1)



On reflection, the naming conventions for the parameter aes.flow and its options "forward" and "backward" might not be the most intuitive. I'd welcome suggestions on that!






share|improve this answer























  • This is fantastic, @Cory; many thanks! Some quick follow-ups: 1) The "-X" argument for the dRG.long assignment resulted in the error "object 'X' not found". Removing this argument worked, however. 2) My resulting plot no longer vertically orders the strata per axis as intended (e.g., workseg, related, unrelated, none); could this be related to the above omission? 3) I assume not, but is it possible to set custom colors for each flow value (beyond the pre-set palettes) or for individual axis strata (rather than for the entire axis)? Thanks again.

    – jjcii
    Mar 29 at 16:57












  • @jjcii glad this helps. (1) and (2) sound like version problems; if you update the three packages (dplyr, ggplot2, ggalluvial) and run the code again, do you see the same behavior? (3) should be possible using scale_fill_manual().

    – Cory Brunson
    Mar 29 at 17:30












  • @jjcii i only responded to the first half of (3); sorry. You can use different colors for the same values at different axes by passing discern = TRUE in to_lodes_form(), which i think is what you're after. Note that the documentation calls this parameter but does not use it in the way you have in mind, so you'll have to see for yourself if it does what you want.

    – Cory Brunson
    Mar 29 at 19:10











  • Thanks, @Cory! With that info I was able to tackle (1), (2), and the first part of (3) successfully. As for the discern argument, setting it to TRUE in the to_lodes_form() function results in following when attempting the plot: "Error: Insufficient values in manual scale. 12 needed but only 4 provided." I've set four colors via a pre-set vector, as recommended for the scale_manual guide (thanks!), but evidently the function is asking for a value for each strata of each axis. Any thoughts?

    – jjcii
    Mar 30 at 17:46












  • @jjcii that's actually what i thought you wanted — a different color for each flow, even flows associated with the same value, when they appear at different axes. Is that not correct? If you leave out discern = TRUE, do you get what you want?

    – Cory Brunson
    Mar 30 at 18:00















1














In the vaccinations example, flows adopt aesthetics from the strata to their lefts via geom_flow() with fill = response (the stratum variable); this can't be done using geom_alluvium(), which renders each complete alluvium as a single graphical object ("grob"). The data you've linked to are in what ggalluvial considers "wide" format, i.e. each axis is a variable, but in order to have a consistent stratum variable the data need to be in "long" format.



The code below makes both of these changes and uses aes.flow = "backward" (see the documentation) to have flows adopt aesthetics from the strata to their rights (instead of their lefts).



library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(ggalluvial)

dRG <- read.csv("~/Downloads/mydata.csv")

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))

dRG.long <- to_lodes_form(dRG.lode, -X,
key = "segment", value = "value", id = "id")

ggplot(dRG.long,
aes(x = segment, stratum = value, alluvium = id)) +
geom_flow(aes(fill = value), width = 1/12, aes.flow = "backward") +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")




Created on 2019-03-28 by the reprex package (v0.2.1)



On reflection, the naming conventions for the parameter aes.flow and its options "forward" and "backward" might not be the most intuitive. I'd welcome suggestions on that!






share|improve this answer























  • This is fantastic, @Cory; many thanks! Some quick follow-ups: 1) The "-X" argument for the dRG.long assignment resulted in the error "object 'X' not found". Removing this argument worked, however. 2) My resulting plot no longer vertically orders the strata per axis as intended (e.g., workseg, related, unrelated, none); could this be related to the above omission? 3) I assume not, but is it possible to set custom colors for each flow value (beyond the pre-set palettes) or for individual axis strata (rather than for the entire axis)? Thanks again.

    – jjcii
    Mar 29 at 16:57












  • @jjcii glad this helps. (1) and (2) sound like version problems; if you update the three packages (dplyr, ggplot2, ggalluvial) and run the code again, do you see the same behavior? (3) should be possible using scale_fill_manual().

    – Cory Brunson
    Mar 29 at 17:30












  • @jjcii i only responded to the first half of (3); sorry. You can use different colors for the same values at different axes by passing discern = TRUE in to_lodes_form(), which i think is what you're after. Note that the documentation calls this parameter but does not use it in the way you have in mind, so you'll have to see for yourself if it does what you want.

    – Cory Brunson
    Mar 29 at 19:10











  • Thanks, @Cory! With that info I was able to tackle (1), (2), and the first part of (3) successfully. As for the discern argument, setting it to TRUE in the to_lodes_form() function results in following when attempting the plot: "Error: Insufficient values in manual scale. 12 needed but only 4 provided." I've set four colors via a pre-set vector, as recommended for the scale_manual guide (thanks!), but evidently the function is asking for a value for each strata of each axis. Any thoughts?

    – jjcii
    Mar 30 at 17:46












  • @jjcii that's actually what i thought you wanted — a different color for each flow, even flows associated with the same value, when they appear at different axes. Is that not correct? If you leave out discern = TRUE, do you get what you want?

    – Cory Brunson
    Mar 30 at 18:00













1












1








1







In the vaccinations example, flows adopt aesthetics from the strata to their lefts via geom_flow() with fill = response (the stratum variable); this can't be done using geom_alluvium(), which renders each complete alluvium as a single graphical object ("grob"). The data you've linked to are in what ggalluvial considers "wide" format, i.e. each axis is a variable, but in order to have a consistent stratum variable the data need to be in "long" format.



The code below makes both of these changes and uses aes.flow = "backward" (see the documentation) to have flows adopt aesthetics from the strata to their rights (instead of their lefts).



library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(ggalluvial)

dRG <- read.csv("~/Downloads/mydata.csv")

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))

dRG.long <- to_lodes_form(dRG.lode, -X,
key = "segment", value = "value", id = "id")

ggplot(dRG.long,
aes(x = segment, stratum = value, alluvium = id)) +
geom_flow(aes(fill = value), width = 1/12, aes.flow = "backward") +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")




Created on 2019-03-28 by the reprex package (v0.2.1)



On reflection, the naming conventions for the parameter aes.flow and its options "forward" and "backward" might not be the most intuitive. I'd welcome suggestions on that!






share|improve this answer













In the vaccinations example, flows adopt aesthetics from the strata to their lefts via geom_flow() with fill = response (the stratum variable); this can't be done using geom_alluvium(), which renders each complete alluvium as a single graphical object ("grob"). The data you've linked to are in what ggalluvial considers "wide" format, i.e. each axis is a variable, but in order to have a consistent stratum variable the data need to be in "long" format.



The code below makes both of these changes and uses aes.flow = "backward" (see the documentation) to have flows adopt aesthetics from the strata to their rights (instead of their lefts).



library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
library(ggalluvial)

dRG <- read.csv("~/Downloads/mydata.csv")

dRG.lode <- dRG %>%
mutate(Seg2 = factor(Seg2, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg3 = factor(Seg3, levels=c("workseg", "related content",
"unrelated content", "NONE")),
Seg4 = factor(Seg4, levels=c("workseg", "related content",
"unrelated content", "NONE")))

dRG.long <- to_lodes_form(dRG.lode, -X,
key = "segment", value = "value", id = "id")

ggplot(dRG.long,
aes(x = segment, stratum = value, alluvium = id)) +
geom_flow(aes(fill = value), width = 1/12, aes.flow = "backward") +
guides(fill = FALSE) +
geom_stratum(width = 1/12, fill = "black", color = "grey") +
geom_label(stat = "stratum", label.strata = TRUE) +
scale_x_discrete(limits = c("Seg1", "Seg2", "Seg3", "Seg4"), expand =
c(.05, .05, .05, .05)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
ggtitle("Time Course, Segment by Segment")




Created on 2019-03-28 by the reprex package (v0.2.1)



On reflection, the naming conventions for the parameter aes.flow and its options "forward" and "backward" might not be the most intuitive. I'd welcome suggestions on that!







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 11:23









Cory BrunsonCory Brunson

1611 silver badge7 bronze badges




1611 silver badge7 bronze badges












  • This is fantastic, @Cory; many thanks! Some quick follow-ups: 1) The "-X" argument for the dRG.long assignment resulted in the error "object 'X' not found". Removing this argument worked, however. 2) My resulting plot no longer vertically orders the strata per axis as intended (e.g., workseg, related, unrelated, none); could this be related to the above omission? 3) I assume not, but is it possible to set custom colors for each flow value (beyond the pre-set palettes) or for individual axis strata (rather than for the entire axis)? Thanks again.

    – jjcii
    Mar 29 at 16:57












  • @jjcii glad this helps. (1) and (2) sound like version problems; if you update the three packages (dplyr, ggplot2, ggalluvial) and run the code again, do you see the same behavior? (3) should be possible using scale_fill_manual().

    – Cory Brunson
    Mar 29 at 17:30












  • @jjcii i only responded to the first half of (3); sorry. You can use different colors for the same values at different axes by passing discern = TRUE in to_lodes_form(), which i think is what you're after. Note that the documentation calls this parameter but does not use it in the way you have in mind, so you'll have to see for yourself if it does what you want.

    – Cory Brunson
    Mar 29 at 19:10











  • Thanks, @Cory! With that info I was able to tackle (1), (2), and the first part of (3) successfully. As for the discern argument, setting it to TRUE in the to_lodes_form() function results in following when attempting the plot: "Error: Insufficient values in manual scale. 12 needed but only 4 provided." I've set four colors via a pre-set vector, as recommended for the scale_manual guide (thanks!), but evidently the function is asking for a value for each strata of each axis. Any thoughts?

    – jjcii
    Mar 30 at 17:46












  • @jjcii that's actually what i thought you wanted — a different color for each flow, even flows associated with the same value, when they appear at different axes. Is that not correct? If you leave out discern = TRUE, do you get what you want?

    – Cory Brunson
    Mar 30 at 18:00

















  • This is fantastic, @Cory; many thanks! Some quick follow-ups: 1) The "-X" argument for the dRG.long assignment resulted in the error "object 'X' not found". Removing this argument worked, however. 2) My resulting plot no longer vertically orders the strata per axis as intended (e.g., workseg, related, unrelated, none); could this be related to the above omission? 3) I assume not, but is it possible to set custom colors for each flow value (beyond the pre-set palettes) or for individual axis strata (rather than for the entire axis)? Thanks again.

    – jjcii
    Mar 29 at 16:57












  • @jjcii glad this helps. (1) and (2) sound like version problems; if you update the three packages (dplyr, ggplot2, ggalluvial) and run the code again, do you see the same behavior? (3) should be possible using scale_fill_manual().

    – Cory Brunson
    Mar 29 at 17:30












  • @jjcii i only responded to the first half of (3); sorry. You can use different colors for the same values at different axes by passing discern = TRUE in to_lodes_form(), which i think is what you're after. Note that the documentation calls this parameter but does not use it in the way you have in mind, so you'll have to see for yourself if it does what you want.

    – Cory Brunson
    Mar 29 at 19:10











  • Thanks, @Cory! With that info I was able to tackle (1), (2), and the first part of (3) successfully. As for the discern argument, setting it to TRUE in the to_lodes_form() function results in following when attempting the plot: "Error: Insufficient values in manual scale. 12 needed but only 4 provided." I've set four colors via a pre-set vector, as recommended for the scale_manual guide (thanks!), but evidently the function is asking for a value for each strata of each axis. Any thoughts?

    – jjcii
    Mar 30 at 17:46












  • @jjcii that's actually what i thought you wanted — a different color for each flow, even flows associated with the same value, when they appear at different axes. Is that not correct? If you leave out discern = TRUE, do you get what you want?

    – Cory Brunson
    Mar 30 at 18:00
















This is fantastic, @Cory; many thanks! Some quick follow-ups: 1) The "-X" argument for the dRG.long assignment resulted in the error "object 'X' not found". Removing this argument worked, however. 2) My resulting plot no longer vertically orders the strata per axis as intended (e.g., workseg, related, unrelated, none); could this be related to the above omission? 3) I assume not, but is it possible to set custom colors for each flow value (beyond the pre-set palettes) or for individual axis strata (rather than for the entire axis)? Thanks again.

– jjcii
Mar 29 at 16:57






This is fantastic, @Cory; many thanks! Some quick follow-ups: 1) The "-X" argument for the dRG.long assignment resulted in the error "object 'X' not found". Removing this argument worked, however. 2) My resulting plot no longer vertically orders the strata per axis as intended (e.g., workseg, related, unrelated, none); could this be related to the above omission? 3) I assume not, but is it possible to set custom colors for each flow value (beyond the pre-set palettes) or for individual axis strata (rather than for the entire axis)? Thanks again.

– jjcii
Mar 29 at 16:57














@jjcii glad this helps. (1) and (2) sound like version problems; if you update the three packages (dplyr, ggplot2, ggalluvial) and run the code again, do you see the same behavior? (3) should be possible using scale_fill_manual().

– Cory Brunson
Mar 29 at 17:30






@jjcii glad this helps. (1) and (2) sound like version problems; if you update the three packages (dplyr, ggplot2, ggalluvial) and run the code again, do you see the same behavior? (3) should be possible using scale_fill_manual().

– Cory Brunson
Mar 29 at 17:30














@jjcii i only responded to the first half of (3); sorry. You can use different colors for the same values at different axes by passing discern = TRUE in to_lodes_form(), which i think is what you're after. Note that the documentation calls this parameter but does not use it in the way you have in mind, so you'll have to see for yourself if it does what you want.

– Cory Brunson
Mar 29 at 19:10





@jjcii i only responded to the first half of (3); sorry. You can use different colors for the same values at different axes by passing discern = TRUE in to_lodes_form(), which i think is what you're after. Note that the documentation calls this parameter but does not use it in the way you have in mind, so you'll have to see for yourself if it does what you want.

– Cory Brunson
Mar 29 at 19:10













Thanks, @Cory! With that info I was able to tackle (1), (2), and the first part of (3) successfully. As for the discern argument, setting it to TRUE in the to_lodes_form() function results in following when attempting the plot: "Error: Insufficient values in manual scale. 12 needed but only 4 provided." I've set four colors via a pre-set vector, as recommended for the scale_manual guide (thanks!), but evidently the function is asking for a value for each strata of each axis. Any thoughts?

– jjcii
Mar 30 at 17:46






Thanks, @Cory! With that info I was able to tackle (1), (2), and the first part of (3) successfully. As for the discern argument, setting it to TRUE in the to_lodes_form() function results in following when attempting the plot: "Error: Insufficient values in manual scale. 12 needed but only 4 provided." I've set four colors via a pre-set vector, as recommended for the scale_manual guide (thanks!), but evidently the function is asking for a value for each strata of each axis. Any thoughts?

– jjcii
Mar 30 at 17:46














@jjcii that's actually what i thought you wanted — a different color for each flow, even flows associated with the same value, when they appear at different axes. Is that not correct? If you leave out discern = TRUE, do you get what you want?

– Cory Brunson
Mar 30 at 18:00





@jjcii that's actually what i thought you wanted — a different color for each flow, even flows associated with the same value, when they appear at different axes. Is that not correct? If you leave out discern = TRUE, do you get what you want?

– Cory Brunson
Mar 30 at 18:00








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















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%2f55347354%2fggalluvial-package-in-r-setting-alluvial-fill-color-axis-by-axis-rather-than%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현