Why does text appear in the legend?

扶醉桌前 提交于 2019-12-25 03:22:56

问题


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)

In the legend, why does 'a' appear alongside, 4,6,8?


回答1:


a symbolizes the text added by geom_label_repel() and it matches the font, colour, etc. of your labels.

The picture below shows one of the demo examples of the ggrepel package shown in the package examples vignette:

You can see the same thing, but with different options passed as arguments to geom_label_repel().

If you actually want to remove the letter "a" from the legend you can redefine the legend key as shown 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


来源:https://stackoverflow.com/questions/53348955/why-does-text-appear-in-the-legend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!