我正在创建一个要在其中显示标签的图ggrepel
。我在下面展示了一个最小的示例,该示例说明了标签如何具有由逗号分隔的两个组件 - 第一个与鸢尾花种类的类型有关,第二个与该组的样本量有关。
# needed libraries
set.seed(123)
library(ggrepel)
# creating a dataframe with label column
(df <- iris %>%
dplyr::group_by(Species) %>%
dplyr::summarise(n = n(), mean = mean(Sepal.Length)) %>%
purrrlyr::by_row(
.d = .,
..f = ~ paste("list(~",
.$Species,
",",
.$n,
")",
sep = ""),
.collate = "rows",
.to = "label",
.labels = TRUE
))
#> # A tibble: 3 x 4
#> Species n mean label
#> <fct> <int> <dbl> <chr>
#> 1 setosa 50 5.01 list(~setosa,50)
#> 2 versicolor 50 5.94 list(~versicolor,50)
#> 3 virginica 50 6.59 list(~virginica,50)
# displaying labels
ggplot(iris, aes(Species, Sepal.Length)) +
geom_point() +
ggrepel::geom_label_repel(data = df,
aes(x = Species, y = mean, label = label),
parse = TRUE)
由reprex 包(v0.2.1)于 2018 年 11 月 17 日创建
我的问题是如何摆脱这两个组件之间的空间。尽管我已经sep = ""
在paste()
函数中指定了,但我不想要的两个组件之间仍然存在额外的空间(例如setosa, 50
,标签应该是, versicolor, 50
, )。virginica, 50
setosa,50
versicolor,50
virginica,50