为什么文字显示在图例中?

时间:2018-11-17 06:51:46

标签: r ggplot2 ggrepel

library(ggplot2)
library(ggrepel)
set.seed(1234)
ss <- sample(1:32, 10)
df <- mtcars[ss, ]

ggplot(df, aes(wt, mpg))+ geom_point(col = "red") + 
  geom_label_repel(aes(label = rownames(df), fill = factor(cyl)), size = 5,
                   hjust = 1,fontface = 3)

在图例中,为什么“ a”出现在4,6,8旁边?enter image description here

1 个答案:

答案 0 :(得分:4)

a象征着geom_label_repel()添加的文本,它与标签的字体,颜色等匹配。

下面的图片显示了包示例插图中显示的ggrepel包的演示示例之一:

enter image description here

您可以看到相同的内容,但是将不同的选项作为参数传递给geom_label_repel()

如果您实际上想从图例中删除字母“ a”,则可以重新定义图例键,如here所示:

# save original legend key for later
oldK <- GeomLabelRepel$draw_key

# define new key without the text label
library(grid)
GeomLabelRepel$draw_key <- function (data, params, size) { draw_key_rect(data) }

# plot
ggplot(df, aes(wt, mpg))+ geom_point(col = "red") + 
  geom_label_repel(aes(label = rownames(df), fill = factor(cyl)), size = 5,
                  fontface = 3)

# reset key
GeomLabelRepel$draw_key <- oldK
相关问题