r - 如何单独更改散点图中图例的填充以匹配标签颜色?
问题描述
如何更改图例中每个键的填充以匹配标签?
如果我使用 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")
)
解决方案
你可以show.legend=F
在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
推荐阅读
- amazon-web-services - 使用 CDK 创建自定义 AWS IAM 策略
- java - 数据库处理程序出现问题会破坏应用程序(kotlin)
- ejabberd - 如何将节从 ejabberd 服务器发送给用户?
- java - 在 jOOQ 中动态创建条件
- angular-dart - 如何使用 dart 控制台对象写入 chrome 控制台?
- c++ - c ++ wxWidgets在尝试访问main之外的wxTextCtrl指针时崩溃
- javascript - 在 JavaScript 中,如何确保数组至少有一个特定元素,而其他元素满足另一个条件?
- javascript - 创建一个与我拥有的元素相似的元素(包括内容和样式)
- amazon-web-services - 是否可以在步进函数的映射状态中遍历 DynamoDB 表?
- python - 如何处理 scipy minimum ValueError: no enough values to unpack (expected 4, got 3)?