Assigning Identical Colours to items in the same group

0

I have been currently working on a solution to this but would like to see if there is already a solution to this that exists. I would like for items within the same group to have identical colours.

For example:

continent <- c("NA", "NA", "Europe", "Europe", "Europe:, "Asia", "Asia", "Asia") country <- c("USA", "Canada", "Germany", "France", "England", "China", "India", "Vietnam")

in this example, for example since USA and Canada are both in NA (North America), I would for example like for the USA to be darker blue and Canada to have a lighter blue. Similarly, since Germany, France and England are all in Europe, I would like Germany to for example be dark red, France to be a lighter red and England to have the lightest red colour. A similar logic can be applied for the Asian countries.

Is there a function in R that does this automatically for a (theoretically) high amount of different groups?

You might be able to do something with the alpha aesthetic of ggplot.

library(ggplot2)
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
DF <- data.frame(continent = c("NA", "NA", "Europe", "Europe", "Europe", "Asia", "Asia", "Asia"), 
                 country = c("USA", "Canada", "Germany", "France", "England", "China", "India", "Vietnam"),
                 Value = c(5,4,7,3,8,4,7,3))

DF <- DF %>% group_by(continent) %>% mutate(Index = LETTERS[1:n()])
ggplot(DF, aes(x = country, y = Value, alpha = Index, fill = continent)) + geom_col() +
  scale_alpha_manual(values = c("A" = 0.4, "B" = 0.6, "C" = 0.8), guide = NULL)

Created on 2021-06-16 by the reprex package (v0.3.0)

Thanks for this! I have been able to adapt this into what I need.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.