ner*_*erd 1 r scatter-plot ggplot2 ggpubr
目前所有点都已标记。如果我只想标记该图中的特定点,而不是所有点,我该如何实现这一点?我想删除所有其他标签,但保留“Herens”、“Payerne”、“Orbe”、“Val de Ruz”、“Lavaux”的标签
data("swiss")
head(swiss)
library(magrittr)
library(dplyr)
library(ggpubr)
# Cmpute MDS
mds <- swiss %>%
dist() %>%
cmdscale() %>%
as_tibble()
colnames(mds) <- c("Dim.1", "Dim.2")
# Plot MDS
ggscatter(mds, x = "Dim.1", y = "Dim.2",
label = rownames(swiss),
size = 1,
repel = TRUE)
Run Code Online (Sandbox Code Playgroud)
电流输出
一种选择是使用“手动”添加标签ggrepel::geom_text_repel
。这允许使用以下方式过滤您想要的类别if_else
:
library(dplyr, warn.conflicts = FALSE)\nlibrary(ggpubr)\n#> Loading required package: ggplot2\nlibrary(ggrepel)\n\n# Cmpute MDS\nmds <- swiss %>%\n dist() %>%\n cmdscale() %>%\n as_tibble()\n#> Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if\n#> `.name_repair` is omitted as of tibble 2.0.0.\n#> \xe2\x84\xb9 Using compatibility `.name_repair`.\ncolnames(mds) <- c("Dim.1", "Dim.2")\n\nmds$label <- rownames(swiss)\n\n# Plot MDS\nggscatter(mds,\n x = "Dim.1", y = "Dim.2",\n size = 1\n) +\n ggrepel::geom_text_repel(\n aes(label = if_else(label %in% c(\'Herens\', \'Payerne\', \'Orbe\', "Val de Ruz", "Lavaux"), label, ""))\n )\n
Run Code Online (Sandbox Code Playgroud)\n