rggplot2sankey-diagramviridis

How to get full range of scale_fill_viridis for sankey plot when using factor


I am trying to create a sankey plot, but also specify in what orders the left nodes will be reported from top to bottom. To specify the order I must change the column by listing it as factor. However, when I do that it changes the scale of colour so that it is more constrained, and the scale_fill_viridis has less distinct colours. It's almost as if it removes the most extreme colour. Can anyone help me get the full range of colour back while still maintaining the order of the left nodes.

My sample data frame is below

df2 = structure(list(x = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L), levels = c("Method_Group", "Discipline"), class = "factor"), 
    node = c("Both + Baited", "Conservation Measures", "Both + Baited", 
    "Other Drivers", "Both + Baited", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Behavioural Ecology", "Receiver + Mobile", 
    "Reproductive Ecology", "Receiver + Mobile", "Other Drivers", 
    "Receiver + Mobile", "Behavioural Ecology", "Receiver + Mobile", 
    "Behavioural Ecology", "Receiver + Mobile", "Methodological", 
    "No Receiver + Animal-borne", "Behavioural Ecology", "No Receiver + Animal-borne", 
    "Methodological", "Receiver + Stationary", "Other Drivers", 
    "Receiver + Stationary", "Resource-Use Management", "Receiver + Stationary", 
    "Reproductive Ecology", "No Receiver + Animal-borne", "Methodological", 
    "No Receiver + Animal-borne", "Reproductive Ecology", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "Receiver + Stationary", "Behavioural Ecology", 
    "Receiver + Stationary", "Fisheries Management", "Receiver + Stationary", 
    "Methodological", "Receiver + Stationary", "Behavioural Ecology", 
    "Receiver + Stationary", "Fisheries Management", "Both + Baited", 
    "Behavioural Ecology", "Both + Baited", "Methodological", 
    "Both + Baited", "Conservation Measures", "No Receiver + Stationary", 
    "Behavioural Ecology", "No Receiver + Stationary", "Other Drivers", 
    "Receiver + Stationary", "Methodological", "Receiver + Stationary", 
    "Reproductive Ecology", "Receiver + Stationary", "Resource-Use Management", 
    "Receiver + Mobile", "Fisheries Management", "Receiver + Stationary", 
    "Behavioural Ecology", "Receiver + Stationary", "Resource-Use Management", 
    "Receiver + Stationary", "Behavioural Ecology", "Receiver + Stationary", 
    "Resource-Use Management", "Receiver + Stationary", "Reproductive Ecology", 
    "Receiver + Baited", "Methodological", "Receiver + Baited", 
    "Reproductive Ecology", "Receiver + Baited", "Conservation Measures", 
    "Receiver + Stationary", "Methodological", "Receiver + Stationary", 
    "Behavioural Ecology", "Receiver + Stationary", "Behavioural Ecology", 
    "Receiver + Stationary", "Conservation Measures", "Receiver + Stationary", 
    "Reproductive Ecology", "Receiver + Stationary", "Behavioural Ecology", 
    "Receiver + Stationary", "Reproductive Ecology", "Receiver + Stationary", 
    "Methodological", "Receiver + Stationary", "Other Drivers", 
    "No Receiver + Animal-borne", "Behavioural Ecology", "No Receiver + Animal-borne", 
    "Methodological", "No Receiver + Stationary", "Other Drivers", 
    "No Receiver + Stationary", "Fisheries Management", "No Receiver + Stationary", 
    "Behavioural Ecology", "No Receiver + Stationary", "Behavioural Ecology", 
    "Receiver + Mobile + Stationary", "Behavioural Ecology", 
    "Receiver + Mobile + Stationary", "Conservation Measures", 
    "Receiver + Baited", "Conservation Measures", "Receiver + Baited", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Methodological", 
    "Both + Mobile", "Reproductive Ecology", "Both + Mobile", 
    "Behavioural Ecology", "Both + Mobile", "Methodological", 
    "Receiver + Stationary", "Reproductive Ecology", "Receiver + Stationary", 
    "Methodological", "Receiver + Mobile", "Fisheries Management", 
    "Receiver + Mobile", "Methodological", "No Receiver + Mobile", 
    "Behavioural Ecology", "No Receiver + Mobile", "Methodological", 
    "No Receiver + Animal-borne", "Other Drivers", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Conservation Measures", 
    "Receiver + Stationary", "Other Drivers", "Receiver + Stationary", 
    "Behavioural Ecology", "Receiver + Stationary", "Methodological", 
    "No Receiver + Animal-borne", "Behavioural Ecology", "No Receiver + Animal-borne", 
    "Methodological", "No Receiver + Animal-borne", "Conservation Measures", 
    "Both + Animal-borne", "Behavioural Ecology", "Both + Animal-borne", 
    "Methodological", "Both + Animal-borne", "Reproductive Ecology", 
    "Both + Animal-borne", "Behavioural Ecology", "Both + Animal-borne", 
    "Methodological", "Receiver + Stationary", "Reproductive Ecology", 
    "Receiver + Stationary", "Behavioural Ecology", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Methodological", 
    "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Other Drivers", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne + Mobile", "Behavioural Ecology", 
    "Both + Stationary", "Behavioural Ecology", "Both + Stationary", 
    "Methodological", "Receiver + Stationary", "Fisheries Management", 
    "Receiver + Stationary", "Other Drivers", "Receiver + Stationary", 
    "Methodological", "Receiver + Stationary", "Conservation Measures", 
    "Receiver + Stationary", "Behavioural Ecology", "Receiver + Stationary", 
    "Methodological", "Receiver + Stationary", "Fisheries Management", 
    "Receiver + Stationary", "Methodological", "Receiver + Stationary", 
    "Methodological", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Other Drivers", "Both + Stationary", "Behavioural Ecology", 
    "Both + Stationary", "Fisheries Management", "Both + Stationary", 
    "Other Drivers", "Receiver + Animal-borne", "Behavioural Ecology", 
    "Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "Receiver + Stationary", "Resource-Use Management", 
    "Receiver + Stationary", "Methodological", "Receiver + Stationary", 
    "Reproductive Ecology", "Receiver + Baited", "Behavioural Ecology", 
    "Receiver + Baited", "Conservation Measures", "Receiver + Baited", 
    "Fisheries Management", "Receiver + Baited", "Other Drivers", 
    "No Receiver + Mobile", "Behavioural Ecology", "No Receiver + Mobile", 
    "Methodological", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Other Drivers", "No Receiver + Animal-borne", "Methodological", 
    "Receiver + Stationary", "Behavioural Ecology", "Both + Stationary", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Other Drivers", "No Receiver + Animal-borne", "Methodological", 
    "Both + Animal-borne", "Behavioural Ecology", "Both + Animal-borne", 
    "Methodological", "Receiver + Mobile", "Methodological", 
    "No Receiver + Animal-borne", "Reproductive Ecology", "No Receiver + Animal-borne", 
    "Methodological", "Receiver + Stationary", "Resource-Use Management", 
    "Receiver + Stationary", "Behavioural Ecology", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Conservation Measures", 
    "No Receiver + Animal-borne", "Reproductive Ecology", "Receiver + Stationary", 
    "Behavioural Ecology", "Receiver + Stationary", "Fisheries Management", 
    "Receiver + Stationary", "Behavioural Ecology", "No Receiver + Animal-borne", 
    "Methodological", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Stationary", "Other Drivers", "No Receiver + Stationary", 
    "Methodological", "No Receiver + Animal-borne", "Reproductive Ecology", 
    "No Receiver + Animal-borne", "Methodological", "Both + Stationary", 
    "Behavioural Ecology", "Both + Stationary", "Methodological", 
    "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Other Drivers", 
    "No Receiver + Stationary", "Methodological", "No Receiver + Stationary", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Methodological", "No Receiver + Animal-borne", 
    "Behavioural Ecology", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Behavioural Ecology", "No Receiver + Animal-borne", 
    "Methodological", "No Receiver + Animal-borne", "Other Drivers", 
    "Receiver + Baited", "Behavioural Ecology", "Receiver + Baited", 
    "Conservation Measures", "Receiver + Baited", "Other Drivers", 
    "Receiver + Stationary", "Behavioural Ecology", "Receiver + Stationary", 
    "Methodological", "Receiver + Stationary", "Fisheries Management", 
    "Receiver + Stationary", "Other Drivers", "No Receiver + Animal-borne", 
    "Reproductive Ecology", "No Receiver + Animal-borne", "Behavioural Ecology", 
    "No Receiver + Animal-borne", "Other Drivers", "Receiver + Mobile", 
    "Behavioural Ecology", "Receiver + Mobile", "Conservation Measures", 
    "Receiver + Stationary", "Behavioural Ecology", "Receiver + Stationary", 
    "Fisheries Management", "Receiver + Stationary", "Methodological", 
    "Receiver + Stationary", "Other Drivers"), next_x = structure(c(2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 
    2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, 
    NA), levels = c("Method_Group", "Discipline"), class = "factor"), 
    next_node = c("Conservation Measures", NA, "Other Drivers", 
    NA, "Behavioural Ecology", NA, "Behavioural Ecology", NA, 
    "Reproductive Ecology", NA, "Other Drivers", NA, "Behavioural Ecology", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Behavioural Ecology", 
    NA, "Methodological", NA, "Other Drivers", NA, "Resource-Use Management", 
    NA, "Reproductive Ecology", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Behavioural Ecology", NA, "Behavioural Ecology", NA, 
    "Fisheries Management", NA, "Methodological", NA, "Behavioural Ecology", 
    NA, "Fisheries Management", NA, "Behavioural Ecology", NA, 
    "Methodological", NA, "Conservation Measures", NA, "Behavioural Ecology", 
    NA, "Other Drivers", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Resource-Use Management", NA, "Fisheries Management", 
    NA, "Behavioural Ecology", NA, "Resource-Use Management", 
    NA, "Behavioural Ecology", NA, "Resource-Use Management", 
    NA, "Reproductive Ecology", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Conservation Measures", NA, "Methodological", NA, "Behavioural Ecology", 
    NA, "Behavioural Ecology", NA, "Conservation Measures", NA, 
    "Reproductive Ecology", NA, "Behavioural Ecology", NA, "Reproductive Ecology", 
    NA, "Methodological", NA, "Other Drivers", NA, "Behavioural Ecology", 
    NA, "Methodological", NA, "Other Drivers", NA, "Fisheries Management", 
    NA, "Behavioural Ecology", NA, "Behavioural Ecology", NA, 
    "Behavioural Ecology", NA, "Conservation Measures", NA, "Conservation Measures", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Methodological", NA, "Fisheries Management", NA, "Methodological", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Other Drivers", 
    NA, "Behavioural Ecology", NA, "Conservation Measures", NA, 
    "Other Drivers", NA, "Behavioural Ecology", NA, "Methodological", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Conservation Measures", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Behavioural Ecology", NA, "Behavioural Ecology", NA, 
    "Methodological", NA, "Methodological", NA, "Behavioural Ecology", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Other Drivers", 
    NA, "Behavioural Ecology", NA, "Behavioural Ecology", NA, 
    "Behavioural Ecology", NA, "Methodological", NA, "Fisheries Management", 
    NA, "Other Drivers", NA, "Methodological", NA, "Conservation Measures", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Fisheries Management", 
    NA, "Methodological", NA, "Methodological", NA, "Behavioural Ecology", 
    NA, "Methodological", NA, "Other Drivers", NA, "Behavioural Ecology", 
    NA, "Fisheries Management", NA, "Other Drivers", NA, "Behavioural Ecology", 
    NA, "Methodological", NA, "Behavioural Ecology", NA, "Resource-Use Management", 
    NA, "Methodological", NA, "Reproductive Ecology", NA, "Behavioural Ecology", 
    NA, "Conservation Measures", NA, "Fisheries Management", 
    NA, "Other Drivers", NA, "Behavioural Ecology", NA, "Methodological", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Other Drivers", 
    NA, "Methodological", NA, "Behavioural Ecology", NA, "Behavioural Ecology", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Other Drivers", 
    NA, "Methodological", NA, "Behavioural Ecology", NA, "Methodological", 
    NA, "Methodological", NA, "Reproductive Ecology", NA, "Methodological", 
    NA, "Resource-Use Management", NA, "Behavioural Ecology", 
    NA, "Behavioural Ecology", NA, "Conservation Measures", NA, 
    "Reproductive Ecology", NA, "Behavioural Ecology", NA, "Fisheries Management", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Behavioural Ecology", 
    NA, "Other Drivers", NA, "Methodological", NA, "Reproductive Ecology", 
    NA, "Methodological", NA, "Behavioural Ecology", NA, "Methodological", 
    NA, "Methodological", NA, "Behavioural Ecology", NA, "Other Drivers", 
    NA, "Methodological", NA, "Behavioural Ecology", NA, "Behavioural Ecology", 
    NA, "Methodological", NA, "Behavioural Ecology", NA, "Behavioural Ecology", 
    NA, "Behavioural Ecology", NA, "Methodological", NA, "Other Drivers", 
    NA, "Behavioural Ecology", NA, "Conservation Measures", NA, 
    "Other Drivers", NA, "Behavioural Ecology", NA, "Methodological", 
    NA, "Fisheries Management", NA, "Other Drivers", NA, "Reproductive Ecology", 
    NA, "Behavioural Ecology", NA, "Other Drivers", NA, "Behavioural Ecology", 
    NA, "Conservation Measures", NA, "Behavioural Ecology", NA, 
    "Fisheries Management", NA, "Methodological", NA, "Other Drivers", 
    NA)), row.names = c(NA, -362L), groups = structure(list(.rows = structure(list(
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
    15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 
    27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
    39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
    51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 
    63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 
    75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 
    87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 
    99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 
    109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 
    119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 
    129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 
    139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 
    149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 
    159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 
    169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 
    179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 
    189L, 190L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 
    199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 
    209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 
    219L, 220L, 221L, 222L, 223L, 224L, 225L, 226L, 227L, 228L, 
    229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L, 238L, 
    239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L, 248L, 
    249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L, 
    259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 
    269L, 270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 
    279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 
    289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 
    299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 
    309L, 310L, 311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 
    319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 
    329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L, 
    339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 
    349L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 
    359L, 360L, 361L, 362L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -362L), class = c("tbl_df", 
"tbl", "data.frame")), class = c("rowwise_df", "tbl_df", "tbl", 
"data.frame"))

This is the code I use to reorder it so that the left node goes in a specific order

df2$node <- factor(
  df2$node,
  levels =
    c("Receiver + Mobile + Stationary", "Both + Mobile",
      "Receiver + Animal-borne", "No Receiver + Animal-borne + Mobile",
       "Both + Baited","No Receiver + Mobile", "Both + Animal-borne","Both + Stationary",
      "Receiver + Baited", "No Receiver + Stationary", "Receiver + Mobile",
      "Receiver + Stationary", "No Receiver + Animal-borne",
    "Reproductive Ecology","Other Drivers","Methodological","Resource-Use Management",
    "Fisheries Management","Conservation Measures","Behavioural Ecology")
)

and this is the code I am using to graph it

remotes::install_github("davidsjoberg/ggsankey")


library(ggsankey)

library("tokenizers")
library(dplyr)
library(tidyr)
library(viridisLite)
library(viridis)
library(ggplot2)



width <- .4

p <- ggplot(df2, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = 1, node.color = "black", show.legend = FALSE, width = width) +
  theme_void() +
  theme(
    plot.margin = unit(rep(5.5, 4), "pt")
  ) +
#I have tried to play around with this code to change scale but to no avail
  scale_fill_viridis_d(begin = 1.0, end = 0)


# Get the data from the flows layer
dat <- layer_data(last_plot(), 1) |>
  filter(x == 2 - width / 2) |>
  distinct(fill, flow_end_ymax, .keep_all = TRUE)

p = p +
  geom_rect(data = dat, aes(
    xmin = x, xmax = x + width,
    ymin = flow_end_ymin, ymax = flow_end_ymax,
    fill = label
  ), inherit.aes = FALSE) +
  geom_sankey_label(size = 5, color = "black", fill = "white") +
  guides(fill = "none") 



p

this is the image I get enter image description here

But I want the colours to have the full range from the yellow to dark blue like this picture enter image description here

Any ideas


Solution

  • The issue is that scale_fill_viridis_d will pick 20 colors, i.e. one color for each category of node. However, using the geom_rect hack you drop the "fill" colors for the 7 final nodes. Hence, to achieve your desired result and show the whole range of viridis colors in the final plot set the limits= in scale_fill_viridis_d so that colors are picked only for the 13 starting nodes:

    library(ggsankey)
    library(ggplot2)
    
    p +
      geom_rect(data = dat, aes(
        xmin = x, xmax = x + width,
        ymin = flow_end_ymin, ymax = flow_end_ymax,
        fill = label
      ), inherit.aes = FALSE) +
      geom_sankey_label(size = 2, color = "black", fill = "white") +
      guides(fill = "none") +
      scale_fill_viridis_d(
        limits = unique(dat$label),
        direction = -1
      )
    

    enter image description here