Color labels appearing as color code in gganimate for ggplot

Viewed 45

I'm graphing data with ggplot and animating it using gganimate. I have colors as my labels and when I add a geom_label_repel() part to my graph it adds the labels but displays the colors as the color code. (Example Blue is displayed as #0000FFFF). Here is a snapshot of my animated graph.

a snapshot of the animated graph

My data is just cumulative percentages for m&m's colors in each bag. Here is what I currently have for my ggplot() and am wondering if I have something wrong or if there is a reason why it's changing the labels from color names to the number codes.

df<- data.frame(x=mm_data$Bag,
                y= c(mm_data$totalp_red,mm_data$totalp_blue,
                     mm_data$totalp_orange,mm_data$totalp_yellow,
                     mm_data$totalp_brown,mm_data$totalp_green),
                group = c(rep("Red", nrow(mm_data)),
                          rep("Blue", nrow(mm_data)),
                          rep("Orange", nrow(mm_data)),
                          rep("Yellow", nrow(mm_data)),
                          rep("Brown", nrow(mm_data)),
                          rep("Green", nrow(mm_data))))
group.colors <- c( "blue3","sandybrown","green3","darkorange"
                  ,"red2","yellow")

  
ggplot(df, aes(x, y, group=group, color=group)) +
  geom_line() +
  geom_point() +
  geom_label_repel(label= df$group,max.overlaps = Inf)+
  scale_color_manual(values = group.colors)+
  ggtitle("Colors present in my M&M Bag") +
  ylab("Distribution percentage") +
  xlab("Bags of M&M's")+
  transition_reveal(x)```
2 Answers

For some reason when the labels are as.character() they will show up like this. All I had to do was save them as as.factor and they appeared as "BLUE", "GREEN", "YELLOW", etc instead of the color code.

  1. Don't use df$ in your ggplot verbs when you are referring to the same data;
  2. You need aes(..), even in geom_label_repel.

Try this.

ggplot(df, aes(x, y, group=group, color=group)) +
  geom_line() +
  geom_point() +
  geom_label_repel(aes(label=group),max.overlaps = Inf)+
  scale_color_manual(values = group.colors)+
  ggtitle("Colors present in my M&M Bag") +
  ylab("Distribution percentage") +
  xlab("Bags of M&M's")+
  transition_reveal(x)
Related