手动添加后缀到ggplot中的图例刻度

Ami*_*Shn 2 r ggplot2

如何手动向 ggplot 中的图例刻度添加任意后缀?

例如,我可以在下面的代码中添加一些内容来生成带有这些独特图例刻度的下面的图吗?

ggplot(iris, aes(Sepal.length, Sepal.Width, color = Species)) + geom_point()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意,我使用 Photoshop 手动将“(s)”添加到图例刻度的末尾。

All*_*ron 5

我们可以使用labels中的参数scale_color_discrete。这可以采用rlang在末尾粘贴“(S)”的 lambda 函数样式。

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + 
  geom_point() +
  scale_color_discrete(labels = ~paste(.x, "(S)"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述