如何在 ggplot2 中标记蜂群图?

AEN*_*ick 2 r ggplot2

我正在尝试将标签添加到我使用 ggplot2 制作的蜂群图中。然而,标签似乎指向中心线,而不是各个点。这是我的代码:

library(ggbeeswarm)
library(tidyverse)

DataTest <- tibble(Category = c(LETTERS),
                   Year = runif(26, 2016, 2016),
                   Size = runif(26, min = 5, max = 10),
                   SalesGrowth = runif(26, -1, 1))

ggplot() + 
  coord_flip() + 
  geom_quasirandom(DataTest,
                   mapping = aes(factor(Year), 
                                 SalesGrowth,
                                 size = Size)) +
  geom_label_repel(DataTest %>% filter(Category %in% c('A', 'B', 'C')),
                   mapping = aes(factor(Year), 
                                 SalesGrowth,
                                 label = Category),
                   box.padding = 2) +
  scale_size_binned() +
  theme(legend.position = "none")
Run Code Online (Sandbox Code Playgroud)

这是输出的视觉效果。我希望我的标签指向相应的点。

示例 ggplot 输出

ste*_*fan 5

这可以像这样实现:

  1. 利用position_quasirandomgeom_label_repel
  2. 作为使用 时的一般规则ggrepel,将整个数据传递给geom_label_repel并将不需要的标签设置为等于,""而不是过滤数据。
library(ggplot2)
library(ggbeeswarm)
library(ggrepel)

DataTest <- data.frame(Category = c(LETTERS),
                   Year = runif(26, 2016, 2016),
                   Size = runif(26, min = 5, max = 10),
                   SalesGrowth = runif(26, -1, 1))

set.seed(42)

ggplot() + 
  coord_flip() + 
  geom_quasirandom(DataTest,
                   mapping = aes(factor(Year), 
                                 SalesGrowth,
                                 size = Size)) +
  geom_label_repel(data = DataTest, mapping = aes(factor(Year), 
                                 SalesGrowth,
                                 label = ifelse(Category %in% c('A', 'B', 'C'), Category, "")),
                   position=position_quasirandom(),
                   box.padding = 2, seed = 42) +
  scale_size_binned() +
  theme(legend.position = "none")
Run Code Online (Sandbox Code Playgroud)