首页 > 解决方案 > 如何单独更改散点图中图例的填充以匹配标签颜色?

问题描述

如何更改图例中每个键的填充以匹配标签?

如果我使用 show.legend = TRUE 在 geom_label_repel 中执行此操作,它看起来不太好,并且会用字母“a”代替点。

在此处输入图像描述

黄色代表受伤球员,蓝色代表自有球员,绿色代表自由球员,红色代表霍比特人球员。

这是用于绘图的代码:

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")
          )

标签: rggplot2

解决方案


你可以show.legend=Fgeom_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

推荐阅读