ggplot colours in tibble

Hi all. I have a df that has the colours already defined as a variable. I would like to reflect these colours in the plot

example <- tribble(
  ~team_name, ~X, ~Y, ~team_colour,
  "red team",   1, 4, "#CC3A00",
  "blue team",   2, 6, "#1E07C5",
  "gream team",   3, 8, "#0AC247"
)

I would like to say something like

ggplot(example, aes(X, Y)) +
  geom_point(aes(colour = team_colour_variable_here))

unfortunately its not so simple,

example <- tribble(
  ~team_name, ~X, ~Y, ~team_colour,
  "red team",   1, 4, "#CC3A00",
  "blue team",   2, 6, "#1E07C5",
  "gream team",   3, 8, "#0AC247"
)

(cols_mapping <- split(unique(example$team_colour),
                       unique(example$team_name)))

ggplot(example, aes(X, Y)) +
  geom_point(aes(colour = team_name)) +
  scale_color_manual(values = cols_mapping)
1 Like

ah perfect, while not as "tidy" as I would have expected, that's pretty concise

Thanks so much :slight_smile:

This is another option using scale_color_identity()

library(tidyverse)

example <- tribble(
    ~team_name, ~X, ~Y, ~team_colour,
    "red team",   1, 4, "#CC3A00",
    "blue team",   2, 6, "#1E07C5",
    "gream team",   3, 8, "#0AC247"
)

ggplot(example, aes(X, Y)) +
    geom_point(aes(colour = team_colour)) +
    scale_color_identity("Team",
                         labels = example$team_name,
                         breaks = example$team_colour,
                         guide = "legend")

Created on 2020-10-27 by the reprex package (v0.3.0.9001)

If you don't need the legend you can just omit the arguments inside scale_color_identity()

I hadnt noticed those identity variants before, thanks for the knowledge andresrcs

Thanks to both of you.

and if I had 2 team colours and wanted to have a fill and colour aes? I could probably use a second geom_point layer or use a shape with a border, ie shape = 21 I guess, but I'm not sure how to apply the manual colour part

example <- tribble(
  ~team_name, ~X, ~Y, ~team_colour, ~team_colour2,
  "red team",   1, 4, "#CC3A00", "#1E07C5",
  "blue team",   2, 6, "#1E07C5", "#FFFFFF",
  "green team",   3, 8, "#0AC247", "#CC3A00"
)

cols_mapping <- split(unique(example$team_colour),
                       unique(example$team_name))

cols_mapping2 <- split(unique(example$team_colour2),
                       unique(example$team_name))

Sorry for the late reply, In case you haven't figured it out yet.

library(tidyverse)

example <- tribble(
    ~team_name, ~X, ~Y, ~team_colour, ~team_colour2,
    "red team",   1, 4, "#CC3A00", "#1E07C5",
    "blue team",   2, 6, "#1E07C5", "#FFFFFF",
    "green team",   3, 8, "#0AC247", "#CC3A00"
)

ggplot(example, aes(X, Y)) +
    geom_point(aes(colour = team_colour, fill = team_colour2), shape = 21) +
    scale_color_identity("Team",
                         labels = example$team_name,
                         breaks = example$team_colour,
                         guide = "legend") +
    scale_fill_identity("Team",
                        labels = example$team_name,
                        breaks = example$team_colour2,
                        guide = "legend")

Created on 2020-10-29 by the reprex package (v0.3.0.9001)

I hadn't. I really appreciate your help, thank you :slight_smile:

This topic was automatically closed 7 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.