在 ggplot 中标记点的顶部

问题描述

library(dplyr)
#Code
mpg %>%
  mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
  ggplot(aes(displ,hwy,colour = Color)) + 
  geom_point()

在上面的代码中,如果我想在 2 轮车的蓝点顶部单独标记 2 轮车而不是图例的单独列,那么我的代码需要修改什么?

解决方法

不确定我是否理解正确,但这是否回答了您的问题?

library(ggplot2)
library(dplyr)
#install.packages("ggrepel")
library(ggrepel)
#Code
mpg %>%
  mutate(Color=ifelse(class == '2seater','2seater','Other')) %>%
  ggplot(aes(displ,hwy,colour = Color)) + 
  geom_point() +
  geom_text_repel(aes(label = ifelse(Color == '2seater',"")),ylim = 35,force_pull = 0,show.legend = FALSE)

example_1.png

或者这个?

library(ggplot2)
library(dplyr)
#install.packages("ggrepel")
library(ggrepel)
#Code
mpg %>%
  mutate(Color=ifelse(class == '2seater',show.legend = FALSE) +
  theme(legend.position = "none")

example_2.png

或者两者的某种组合?