Removing ggplot legend symbol while retaining label

ぃ、小莉子 提交于 2019-12-10 12:43:26

问题


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")))

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).

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)

回答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:


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

  1. Remove the geom_text legend.
  2. 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.
  3. Override the size of the point markers in the legend, so that point markers will appear in the legend key to distinguish each group.

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)))



来源:https://stackoverflow.com/questions/46005015/removing-ggplot-legend-symbol-while-retaining-label

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