1

Is there any command to set your own pallet?

In a plot like this:

library(ggalluvial)
library(ggplot2)
library(dplyr)

df <- data.frame(status = c("open", "close", "close", "open/close", "close"), 
                 stock = c("google", "amazon", "amazon", "yahoo", "amazon"), 
                 newspaper = c("times", "newyork", "london", "times", "times"))

# Count the number of occurance for each alluvial
df <- df %>% dplyr::group_by(stock, newspaper, status) %>% 
  summarise(n = n()) 

# Define the factors
df$status <- factor(df$status, levels = c("open", "open/close", "close"))
df$stock <- factor(df$stock, levels = c("google", "amazon", "yahoo"))
df$newspaper <- factor(df$newspaper, levels = c("times", "newyork", "london"))

# Plot the alluvial as in https://cran.r-project.org/web/packages/ggalluvial/vignettes/ggalluvial.html#alluvia-wide-format
ggplot2::ggplot(df.2, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  ggalluvial::geom_alluvium(aes(fill = status), width = 1/12) +
  ggalluvial::geom_stratum(width = 1/12, fill = "black", color = "grey") +
  ggplot2::geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  ggplot2::scale_x_discrete(limits = c("stock", "newspaper"), expand = c(.05, .05)) +
  ggplot2::scale_fill_brewer(type = "qual", palette = "Set1")

How is it possible to set our own colours?

Erik Brole
  • 315
  • 9
  • Do you mean to say `palette = "Set1"` have no effect? (didn't test your code yet) – zx8754 Dec 12 '22 at 10:26
  • @zx8754 it has effect but I would like to set my own colours as it supports until 9 cases for colours – Erik Brole Dec 12 '22 at 10:27
  • See this post to create most distinct colour sets: https://stackoverflow.com/q/15282580/680068 – zx8754 Dec 12 '22 at 10:34
  • @zx8754 my problem is that this palette = "Set2" provides until 8 cases and I have 12 cases please could you help me if there is an easy way to set in this command ggplot2::scale_fill_brewer(type = "qual", palette = "Set1") the option you refer – Erik Brole Dec 12 '22 at 10:49
  • Allan gave you solution with 3 colours, replace it with 8 colours. See https://stackoverflow.com/q/15282580/680068 this post to create big number of distinct colours. – zx8754 Dec 12 '22 at 11:15

1 Answers1

1

You already are setting your own palette (you have chosen the "Set1" palette from the RColorBrewer package via scale_fill_brewer). If you want to set the colors manually, use scale_fill_manual

ggplot(df, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  geom_alluvium(aes(fill = status), width = 1/12) +
  geom_stratum(width = 1/12, fill = "gray30", color = "white", size = 2) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum), angle = 90),
            color = "white", size = 6) +
  scale_x_discrete(limits = c("stock", "newspaper"), expand = c(.05, .05)) +
  scale_fill_manual(values = c(open = 'red4', 
                               `open/close` = 'orange3', 
                               close ='navy')) +
  ggtitle("Alluvial-Test") +
  theme_void(base_size = 16) +
  theme(plot.margin = margin(20, 20, 20, 20))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87