【问题标题】:Labeling in ggplot on top of point在 ggplot 中标记点顶部
【发布时间】:2021-04-09 09:44:27
【问题描述】:
library(dplyr)
#Code
mpg %>%
  mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
  ggplot(aes(displ, hwy, colour = Color)) + 
  geom_point()

在上面的代码中,如果我想在 2 Wheeler 的蓝点顶部单独标记 2 Wheeler 而不是单独的图例列,请对我的代码进行什么修改?

【问题讨论】:

  • 您想在蓝点上添加什么标签?您可以通过例如添加标签geom_text(aes(label = XXX))geom_label.

标签: r ggplot2 labeling


【解决方案1】:

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

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', '2seater', "")),
                   ylim = 35, force_pull = 0, show.legend = FALSE)

或者这个?

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', '2seater', "")),
                   force_pull = 0, show.legend = FALSE) +
  theme(legend.position = "none")

还是两者的结合?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多