在保留标签的同时删除ggplot图例符号

时间:2022-05-17 02:12:53

Example code and figure:

示例代码和图:

data <- data.frame( ID = c(LETTERS[1:26], paste0("A",LETTERS[1:26])),
                    Group = rep(c("Control","Treatment"),26),
                    x = rnorm(52,50,20),
                    y = rnorm(52,50,10))

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
   geom_text(size=8) + 
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   theme(legend.text = element_text(color=c("blue","red")))

在保留标签的同时删除ggplot图例符号

What I'm trying to solve is removing the legend symbols (the "a") and coloring the Group labels (Control and Treatment) as they appear in the plot (Blue and Red respectively).

我正在尝试解决的是删除图例符号(“a”)并为组标签(控制和处理)着色,因为它们出现在图中(分别为蓝色和红色)。

I've tried:

geom_text(show_guide = F)

But that just removes the legend entirely.

但这完全取消了传说。

To keep it simple I could just use annotate...but wondering if there's a legend specific solution.

为了保持简单,我可以使用注释......但是想知道是否有特定于图例的解决方案。

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) +
   geom_text(size=8, show_guide=F) +
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   annotate("text",label="Control", color="blue",x=20,y=80,size=8) +
   annotate("text",label="Treatment", color="Red",x=23,y=77,size=8)

2 个解决方案

#1


1  

As a quick fix you can tweak the legend key, by hard coding the info you want, although around the other way - keep the key and remove the label.

作为一个快速修复,您可以通过硬编码您想要的信息来调整图例键,尽管在另一方面 - 保留密钥并删除标签。

library(grid)

GeomText$draw_key <- function (data, params, size) {
    txt <- ifelse(data$colour=="blue", "Control", "Treatment") 
    # change x=0 and left justify 
    textGrob(txt, 0, 0.5,  
             just="left", 
             gp = gpar(col = alpha(data$colour, data$alpha), 
                       fontfamily = data$family, 
                       fontface = data$fontface, 
                       # also added 0.5 to reduce size
                       fontsize = data$size * .pt* 0.5))
}

And when you plot you suppress the legend labels, and make legend key a bit wider to fit text.

当您绘制时,您可以抑制图例标签,并使图例键更宽一些以适应文本。

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
   geom_text(size=8) + 
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   theme(legend.text = element_blank(),
         legend.key.width = unit(1.5, "cm"))

#2


3  

Another option is to use point markers (instead of the letter "a") as the legend symbols, which you can do with the following workaround:

另一种选择是使用点标记(而不是字母“a”)作为图例符号,您可以使用以下解决方法:

  1. Remove the geom_text legend.
  2. 删除geom_text图例。

  3. Add a "dummy" point geom and set the point marker size to NA, so no points are actually plotted, but a legend will be generated.
  4. 添加“虚拟”点geom并将点标记大小设置为NA,因此实际上不会绘制任何点,但会生成图例。

  5. Override the size of the point markers in the legend, so that point markers will appear in the legend key to distinguish each group.
  6. 覆盖图例中点标记的大小,以便点标记将出现在图例键中以区分每个组。


ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
  geom_text(size=8, show.legend=FALSE) + 
  geom_point(size=NA) +
  scale_color_manual(values=c("blue","red")) +
  theme_classic() +
  labs(colour="") +
  guides(colour=guide_legend(override.aes=list(size=4)))

在保留标签的同时删除ggplot图例符号

#1


1  

As a quick fix you can tweak the legend key, by hard coding the info you want, although around the other way - keep the key and remove the label.

作为一个快速修复,您可以通过硬编码您想要的信息来调整图例键,尽管在另一方面 - 保留密钥并删除标签。

library(grid)

GeomText$draw_key <- function (data, params, size) {
    txt <- ifelse(data$colour=="blue", "Control", "Treatment") 
    # change x=0 and left justify 
    textGrob(txt, 0, 0.5,  
             just="left", 
             gp = gpar(col = alpha(data$colour, data$alpha), 
                       fontfamily = data$family, 
                       fontface = data$fontface, 
                       # also added 0.5 to reduce size
                       fontsize = data$size * .pt* 0.5))
}

And when you plot you suppress the legend labels, and make legend key a bit wider to fit text.

当您绘制时,您可以抑制图例标签,并使图例键更宽一些以适应文本。

ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
   geom_text(size=8) + 
   scale_color_manual(values=c("blue","red")) +
   theme_classic() +
   theme(legend.text = element_blank(),
         legend.key.width = unit(1.5, "cm"))

#2


3  

Another option is to use point markers (instead of the letter "a") as the legend symbols, which you can do with the following workaround:

另一种选择是使用点标记(而不是字母“a”)作为图例符号,您可以使用以下解决方法:

  1. Remove the geom_text legend.
  2. 删除geom_text图例。

  3. Add a "dummy" point geom and set the point marker size to NA, so no points are actually plotted, but a legend will be generated.
  4. 添加“虚拟”点geom并将点标记大小设置为NA,因此实际上不会绘制任何点,但会生成图例。

  5. Override the size of the point markers in the legend, so that point markers will appear in the legend key to distinguish each group.
  6. 覆盖图例中点标记的大小,以便点标记将出现在图例键中以区分每个组。


ggplot(data, aes(y=y,x=x, label=ID, color=Group)) + 
  geom_text(size=8, show.legend=FALSE) + 
  geom_point(size=NA) +
  scale_color_manual(values=c("blue","red")) +
  theme_classic() +
  labs(colour="") +
  guides(colour=guide_legend(override.aes=list(size=4)))

在保留标签的同时删除ggplot图例符号