Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom label colours in geom_label_repel using hex codes #82

Closed
JoGall opened this issue Aug 10, 2017 · 2 comments
Closed

Custom label colours in geom_label_repel using hex codes #82

JoGall opened this issue Aug 10, 2017 · 2 comments

Comments

@JoGall
Copy link

JoGall commented Aug 10, 2017

First of all: thanks for a great package, you've saved me endless annoyance and wasted time fiddling around with plot labels!

I'm trying to apply custom colours to my gg_label_repel boxes, labels and borders using hexadecimal codes stored in my dataset (each x,y pair has corresponding hex codes col1, col2).

In vanilla ggplot I can colour the points how I want like so:

ggplot(d, aes(x = x, y = y)) +
  geom_point(size = 5, pch = 21, stroke = 3, colour = d$col1, fill = d$col2)

Producing a plot like this:
ggplot_ex

But when I try to colour the boxes and labels in geom_label_repel like so:

ggplot(d, aes(x = height, y = fouls)) +
  geom_point(size = 3) +
  geom_label_repel(
    aes(label = team, fill = col1, colour = col2),
    fontface = 'bold',
    box.padding = unit(0.35, "lines"),
    point.padding = unit(0.5, "lines"),
    segment.color = 'grey50'
  )

I get this undesired result:
ggrepel_ex

It looks like colour is being treated as a factor and the actual hexadecimal code not being parsed? I've tried specifying fill = col1, colour = col2 in the ggplot aesthetics call, various arguments in scale_fill_manual, and casting the variables col1 and col2 as a character but nothing has worked so far.

Here's a sample of the data returned by dput:

structure(list(team = structure(c(1L, 2L, 4L, 5L, 6L), .Label = c("AFC Bournemouth", 
"Arsenal", "Brighton & Hove Albion", "Burnley", "Chelsea", "Crystal Palace", 
"Everton", "Huddersfield Town", "Hull City", "Leicester City", 
"Liverpool", "Manchester City", "Manchester United", "Middlesbrough", 
"Newcastle United", "Southampton", "Stoke City", "Sunderland", 
"Swansea City", "Tottenham Hotspur", "Watford", "West Bromwich Albion", 
"West Ham United"), class = "factor"), col1 = structure(c(15L, 
18L, 8L, 5L, 11L), .Label = c("#000000", "#0053A0", "#0054A6", 
"#0072CE", "#034694", "#091453", "#274488", "#53162F", "#5CBFEB", 
"#60223B", "#C4122E", "#D00027", "#DA020E", "#E03A3E", "#E62333", 
"#EB172B", "#ED1A3B", "#EF0107", "#F5A12D", "#FBEE23", "#FF0000", 
"#FFFFFF"), class = "factor"), col2 = structure(c(1L, 4L, 7L, 
9L, 5L), .Label = c("#000000", "#001C58", "#0072CE", "#023474", 
"#1B458F", "#5299C6", "#8CCCE5", "#FDBE11", "#FFFFFF"), class = "factor"), 
    height = c(179.2, 179.3, 180.2, 181.5, 185.1), fouls = c(368L, 
    398L, 429L, 395L, 470L)), .Names = c("team", "col1", "col2", 
"x", "y"), row.names = c(NA, 5L), class = "data.frame")

I've scoured the vignette and StackOverflow to not avail, but maybe I'm missing something obvious..

Thanks in advance!

@JoGall
Copy link
Author

JoGall commented Aug 13, 2017

No matter, solved it myself using a combination of scale_fill_manual and scale_colour_manual!

ggplot(dat, aes(x = x, y = y)) +
  geom_point(size = 2) +
  geom_smooth(method='lm', col="black", alpha=0.25) +
  xlab("Fouls committed") +
  ylab("Discipline score") +

  geom_label_repel(
    aes(label = team, fill = team, color = team),
    segment.colour = "black",
    fontface = 'bold',
    box.padding = unit(0.35, "lines"),
    point.padding = unit(0.5, "lines")
    ) +
  scale_fill_manual(values = setNames(dat$col1, levels(dat$team))) +
  scale_color_manual(values = setNames(dat$col2, levels(dat$team)))
  my_theme

ex2

@JoGall JoGall closed this as completed Aug 13, 2017
@slowkow
Copy link
Owner

slowkow commented Aug 14, 2017

Thanks for posting your problem, a figure, and your solution!


It turns out that this issue is not related to ggrepel specifically, but applies to any ggplot2 plot.

Here's an example showing the two ways you used to specify colors. I hope it helps to clarify the issue.

d <- structure(list(
  team = structure(
    c(1L, 2L, 4L, 5L, 6L),
    .Label = c("AFC Bournemouth",
               "Arsenal",
               "Brighton & Hove Albion",
               "Burnley",
               "Chelsea",
               "Crystal Palace",
               "Everton",
               "Huddersfield Town",
               "Hull City",
               "Leicester City",
               "Liverpool",
               "Manchester City",
               "Manchester United",
               "Middlesbrough",
               "Newcastle United",
               "Southampton",
               "Stoke City",
               "Sunderland",
               "Swansea City",
               "Tottenham Hotspur",
               "Watford",
               "West Bromwich Albion",
               "West Ham United"),
    class = "factor"),
  col1 = structure(
    c(15L, 18L, 8L, 5L, 11L),
    .Label = c(
      "#000000", "#0053A0", "#0054A6",
      "#0072CE", "#034694", "#091453", "#274488", "#53162F", "#5CBFEB",
      "#60223B", "#C4122E", "#D00027", "#DA020E", "#E03A3E", "#E62333",
      "#EB172B", "#ED1A3B", "#EF0107", "#F5A12D", "#FBEE23", "#FF0000",
      "#FFFFFF"),
    class = "factor"),
  col2 = structure(c(
    1L, 4L, 7L, 9L, 5L),
    .Label = c("#000000", "#001C58", "#0072CE", "#023474",
               "#1B458F", "#5299C6", "#8CCCE5", "#FDBE11", "#FFFFFF"),
    class = "factor"),
  height = c(179.2, 179.3, 180.2, 181.5, 185.1),
  fouls = c(368L,398L, 429L, 395L, 470L)),
  .Names = c("team", "col1", "col2","y"),
  row.names = c(NA, 5L),
  class = "data.frame"
)

library(ggplot2)

Example 1

If you pass colors to geom_*, then they're expected to be color values such as names of colors or hexadecimal colors. The values will be used directly to color the plot.

# Here, we're passing the colors to the geom_point() function.
ggplot(d, aes(x = x, y = y)) +
  geom_point(size = 5, pch = 21, stroke = 3, colour = d$col1, fill = d$col2)

image

Example 2

If you pass values to the aes() function, then they're interpreted as numbers or factors, not as literal color values. Then, ggplot2 will assign colors automatically, or you can use scale_color_* and scale_fill_* to change the assigned colors.

# Here, we're passing the colors into the aes() function.
ggplot(d, aes(x = x, y = y, colour = col1, fill = col2)) +
  geom_point(size = 5, pch = 21, stroke = 3)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants