简体   繁体   中英

change the key labels in a legend in ggplot2

I tryed to change key labels on ggplot , but I was unsuccessful. When I indicate labels at scale_color_manual line the legend appears duplicated. Where is my mistake?

Consider de example:

mydata <- data.frame(
year=as.integer(rep(2010:2020,each=2)),
type=rep(c("a","b"),11),
value=c(617,186,546,241,430,217,349,188,286,141,446,166,442,167,424,210,421,182,405,190,432,194))

ggplot(mydata,aes(year,value,group=type))+
    theme_bw()+
    theme(
        axis.text=element_text(size=16),
        axis.title=element_text(size=18),
        legend.position=c(.75,.885),
        legend.key = element_rect(color = "white", fill = NA),
        legend.key.size = unit(1, "cm"),
        legend.title=element_blank(),
        legend.text=element_text(size=20)
    )+
    labs(x="year",y="number")+
    geom_point(aes(color=type,shape=type),size=3)+
    scale_x_continuous(breaks = seq(min(mydata$year),max(mydata$year), by = 2))+
    scale_shape_manual(values=c(15,19))+
    scale_color_manual(values=c("red","blue"))

But if I replace legend key "a" and "b" with "group a" and "group b" with

scale_color_manual(values=c("red","blue"),labels=c("group a","group b"))

I get duplicated legends and the colored bullets become wrong.

wrong plot

Whats going wrong?

Thanks!

The issue is caused by changing the color labels but not the shape labels. So you either need to apply the labels to both shape and color or change the type factor labels before plotting.

library(ggplot2)
library(dplyr)

mydata %>%
  mutate(type = factor(type, labels = c("group a","group b"))) %>%
  ggplot(aes(year,value))+
  theme_bw()+
  theme(
    axis.text=element_text(size=16),
    axis.title=element_text(size=18),
    legend.position=c(.75,.885),
    legend.key = element_rect(color = "white", fill = NA),
    legend.key.size = unit(1, "cm"),
    legend.title=element_blank(),
    legend.text=element_text(size=20)
  )+
  labs(x="year",y="number")+
  geom_point(aes(color=type,shape=type),size=3)+
  scale_x_continuous(breaks = seq(min(mydata$year),max(mydata$year), by = 2))+
  scale_shape_manual(values=c(15,19))+
  scale_color_manual(values=c("red","blue"))

在此处输入图片说明

You can do this without changing the factor levels, provided you add the same labels to both the colour and shape scales:

ggplot(mydata,aes(year,value,group=type))+
    theme_bw()+
    theme(
        axis.text=element_text(size=16),
        axis.title=element_text(size=18),
        legend.position=c(.75,.885),
        legend.key = element_rect(color = "white", fill = NA),
        legend.key.size = unit(1, "cm"),
        legend.title=element_blank(),
        legend.text=element_text(size=20)
    )+
    labs(x="year",y="number")+
    geom_point(aes(color=type,shape=type),size=3)+
    scale_x_continuous(breaks = seq(min(mydata$year),max(mydata$year), by = 2))+
    scale_shape_manual(values=c(15,19), labels = c("group a", "group b")) +
    scale_color_manual(values=c("red","blue"), labels = c("group a", "group b"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM