[英]change the key labels in a legend in ggplot2
I tryed to change key labels on ggplot
, but I was unsuccessful.我尝试更改
ggplot
上的关键标签,但没有成功。 When I indicate labels at scale_color_manual
line the legend appears duplicated.当我在
scale_color_manual
行指示标签时,图例出现重复。 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但是如果我用“group a”和“group b”替换图例键“a”和“b”
scale_color_manual(values=c("red","blue"),labels=c("group a","group b"))
I get duplicated legends and the colored bullets become wrong.我得到了重复的图例,彩色的子弹变错了。
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.因此,您要么需要将标签应用于形状和颜色,要么在绘图之前更改
type
因子标签。
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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.