geom_label with custom background and color (text)

I have a geom_point ggplot and I'm trying to employ custom backgrounds and colors (text). I've written a couple of simple function to set the background and color. Here is my plot code.

p<-ggplot(tchart, aes(x = beatenLengths,y = OffRail)) +
  geom_point() + 
  geom_label(aes(label=pgm, fill=map(pgm,pgmcolorb), color=map(pgm,pgmcolor)),  size = 2.5) +
  geom_convexhull(alpha = 0.2, fill = "blue") +
  facet_wrap(~ Sixteenth, ncol=4) +

When I run this code a get the following errors numerous times. The chart is ultimately rendered, but there is no text and the labels are distorted. See below.

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :

supplied color is neither numeric nor character

Warning in, vp, TRUE): supplied color is

neither numeric nor character

Alternatively, if I run the code with the geom_label below the chart works, but I have no custom colors - I need custom colors.

geom_label(aes(label=pgm, fill=map(pgm,pgmcolorb)), size = 2.5, color="white") +

How can I structure my geom_label to get both custom backgrounds and colors?badchart

Hi @mutuelinvestor. You may custom the color of fill and color by scale_fill_manual and scale_color_manual like the following code.


df <- data.frame(x = sample(1:50, 5), y = sample(1:50, 5), group = LETTERS[1:5], stringsAsFactors = FALSE)

df %>%
  ggplot(aes(x = x, y = y)) +
  geom_point() +
  geom_label(aes(label = group, fill = group, color = group), size = 2.5) +
  ggConvexHull::geom_convexhull(alpha = 0.2, fill = "blue") +
  scale_fill_manual(values = RColorBrewer::brewer.pal(5, "Spectral")) +
  scale_color_manual(values = rev(RColorBrewer::brewer.pal(5, "Spectral")))

Created on 2019-11-20 by the reprex package (v0.3.0)


@raytong, Thanks for the reply. This is very helpful. One point of clarification. I need to assign specific colors to specific values. For example, my function pgmcolor returns a color based upon the value of pgm. Given that, would the following be the approach I should use:
scale_color_manual(values =map(pgm,pgmcolor) )
scale_fill_manual(values =map(pgm,pgmcolorb) )

Or, do I specifically need to use RColorBrewer?


@mutuelinvestor. You can use your own function to assign color. But the values pass to your map function will be changed because the columns of tchart cannot map outside aes. If pgmcolor and pgmcolorb are the column name in tchart, the code will be scale_color_manual(values=map(tchart$pgm, tchart$pgmcolor)). If pgmcolor and pgmcolorb are variables, the code will be scale_color_manual(values=map(tchart$pgm, pgmcolor)).

@raytong -I'm getting an error message: continuous value supplied to discrete scale.

@mutuelinvestor. The error because your map function return a numeric vector, not colour code. So, you can only use RColorBrewer or pass a vector of RGB color code to it.

Here is the code that finally worked. I had to use as.factor() and then used hex values. Thanks to @raytong for all your help.

p<-ggplot(tchart, aes(x = beatenLengths,y = OffRail)) +
  geom_point() + 
  geom_label(aes(label=pgm, fill= as.factor(pgm), color= as.factor(pgm)),  size = 3.0) +
  geom_convexhull(alpha = 0.2, fill = "blue") +
  scale_fill_manual(values = c("#ec2c28","#FFFFFF","#1d4fa3","#EAE824","#458544","#060103","#EFA428","#f9bcc5", "#1FB8D7","#9A4687","#C2C2C2","#a9d5b5")) +
  scale_color_manual(values = c("#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#EAE824", "#000000", "#000000","#000000","#000000","#000000","#000000")) +
  facet_wrap(~ Sixteenth, ncol=4) +
  theme_fivethirtyeight() +
  theme(legend.position = "none")
1 Like

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