为什么文字显示在图例中? [英] Why does text appear in the legend?

查看:59
本文介绍了为什么文字显示在图例中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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旁边?

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

推荐答案

a 表示 geom_label_repel()添加的文本,并且与字体,颜色等匹配.标签.

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

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

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

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

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

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

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

这篇关于为什么文字显示在图例中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆