-1

How can I change the filling of each key in the legend to match the labels?

If I do it in geom_label_repel using show.legend = TRUE it doesn't look very good and it puts a letter "a" in place of dots.

enter image description here

Yellow is for injured players, blue for owned players, green for free players and red for hobbits players.

Here's the code used for the plot:

ggplot(fim, aes(Price, Average, 
                      label = Player, 
                      colour = color, 
                      fill = color,
                      #alpha = ih, 
                      group = Position
              )) +
    geom_smooth(method = "lm", se = FALSE, color = "lightblue", show.legend = FALSE) +
    geom_point(aes(fill = factor(color))) + # 
    geom_label_repel(size = 3.25,
                     family = "Bahnschrift",
                     #fontface = 'bold',
                     label.size = NA,
                     segment.alpha = 0.5,
                     alpha = 0.9,
                     show.legend = FALSE,
                     #label.padding = unit(.22, 'lines'),
                     #hjust = 0,
                     #vjust = 0,
                     #label.r = 0,
                     box.padding = unit(0.20, "lines"),
                     point.padding = unit(0.20, "lines"),
                     #force = 4
                     ) +
    #nudge_y = 0.005,
    #nudge_x = 0) +
    scale_x_continuous(labels=function(y) format(y, big.mark = ".", scientific = FALSE)) +
    ggtitle("Price and Average Points in LaLiga Fantasy",
            paste("Top", nrow(fim), pos, "by market value with at least", minapps, "appearances, excluding Messi & Benzema")) +
    labs(y="Average Points (Sofascore Rating System)",
         x = "Price (Market Value in Euros)",
         caption = "Sources: Biwenger, Jornada Perfecta plot by Weldata") +
    scale_color_manual(values = c("Hobbits" = WT,
                                  "Free" = WT,
                                  "Injured" = BK,
                                  "Owned" = WT)) +
    scale_fill_manual(values = c("Hobbits" = cl,
                                 "Free" = MF,
                                 "Injured" = GK,
                                 "Owned" = DF)) +
    scale_alpha(0.1) +
    dark_theme_gray() +
    theme(plot.title = element_text(family = "Bahnschrift", 
                                    face = "bold", 
                                    size = 18, 
                                    colour = YL),
          plot.background = element_rect(fill = BK),
          panel.background = element_blank(),
          panel.grid.major = element_line(color = "grey30", size = 0.2),
          panel.grid.minor = element_line(color = "grey30", size = 0.2),
          legend.title = element_blank(),
          #legend.background = element_blank(),
          axis.ticks = element_line(colour = "grey30"),
          axis.title = element_text(family = "Bahnschrift", size = 14, colour = WT),
          axis.text = element_text(size = 12, colour = "grey80", face = 'bold'),
          legend.position = c(0.9, 0.2), #legend.position = "none",
          plot.tag = element_text(),
          plot.caption = element_text(color = YL, face = "italic")
          )
pppery
  • 3,731
  • 22
  • 33
  • 46
Boromir
  • 99
  • 1
  • 1
  • 5
  • You can add a reproductible example: `library(ggrepel) set.seed(42) dat <- subset(mtcars, wt > 2.75 & wt < 3.45) dat$car <- rownames(dat) p <- ggplot(dat, aes(wt, mpg, label = car)) + geom_point(aes(color=car),shape=3)+ geom_label_repel(aes( fill=car)) + labs(title = "geom_text_repel()") p` – Armando González Díaz Feb 20 '20 at 15:37

1 Answers1

0

You can use show.legend=F in geom_label_repel

library(ggrepel)
set.seed(42)

dat <- subset(mtcars, wt > 2.75 & wt < 3.45)
dat$car <- rownames(dat)
# your problem:
p <- ggplot(dat, aes(wt, mpg, label = car)) +
  geom_point(aes(color=car))+ 
  geom_label_repel(aes( fill=car)) +

  labs(title = "geom_text_repel()")
p
#Answer:
p <- ggplot(dat, aes(wt, mpg, label = car)) +
  geom_point(aes(color=car))+ 
  geom_label_repel(aes( fill=car), show.legend  = F) +

  labs(title = "geom_text_repel()")
p