Legends
本文档介绍ggplot绘图中对legend的修改。在文档开始之前,先看看ggplot2默认的图例:
library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
bp
2
3
# 1. 删除legend
隐藏图例,首先要看图中用来区分不同分组的方式,比如在上面boxplot的例子中,使用fill
的颜色来区分,那么想要删除图例,也需要针对fill
做相关改动。
这里有3中方式:
# Remove legend for a particular aesthetic (fill)
bp + guides(fill = FALSE)
# 同理还可以有:
# bp + guides(color = FALSE)
# bp + guides(shape = FALSE)
# bp + guides(size = FALSE)
# ...
# It can also be done when specifying the scale
bp + scale_fill_discrete(guide=FALSE)
# 同理还可以有:
# bp + scale_color_discrete(guide=FALSE)
# bp + scale_shapediscrete(guide=FALSE)
# ...
# This removes all legends, 通用方法⭐️⭐️
bp + theme(legend.position="none")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 2. 修改legend项目的顺序
bp + scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))
2
根据指定颜色的方式,使用不同的scale方法,例如 scale_fill_manual
、scale_colour_hue
、scale_colour_manual
、scale_shape_discrete
、scale_linetype_discrete
等。
# 3. 反转legend的顺序
# These two methods are equivalent:
bp + guides(fill = guide_legend(reverse=TRUE))
bp + scale_fill_discrete(guide = guide_legend(reverse=TRUE))
# You can also modify the scale directly:
bp + scale_fill_discrete(breaks = rev(levels(PlantGrowth$group)))
2
3
4
5
6
根据指定颜色的方式,使用不同的scale方法,例如 scale_fill_manual
、scale_colour_hue
、scale_colour_manual
、scale_shape_discrete
、scale_linetype_discrete
等。
# 4. 隐藏legend标题
以下两种方式可以隐藏legend title。
# Remove title for fill legend
bp + guides(fill=guide_legend(title=NULL))
# Remove title for all legends
bp + theme(legend.title=element_blank())
2
3
4
5
# 5. 修改legend标题与标签
有两种方法可以更改图例标题和标签的内容。第一种方法是设定scale
相关参数;第二种方法是直接更改原始数据相应factor的name(对应title) 或 levels(对应label)。
# 5.1. 使用scale方式(推荐)
legend可以展示图中fill
,colour
,linetype
,shape
,size
等分组信息,可以用scale_xxx_xxx
对legend进行修改。
bp + scale_fill_discrete(name="Experimental\nCondition") + ggtitle("Change legend title")
bp + scale_fill_discrete(name="Experimental\nCondition",
breaks=c("ctrl", "trt1", "trt2"),
labels=c("Control", "Treatment 1", "Treatment 2")) +
ggtitle("Change legend title & labels")
# Using a manual scale instead of hue
bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"),
name="Experimental\nCondition",
breaks=c("ctrl", "trt1", "trt2"),
labels=c("Control", "Treatment 1", "Treatment 2")) +
ggtitle("Change legend title & labels & fill color")
2
3
4
5
6
7
8
9
10
11
12
13
值得注意的是,以上操作不会改变坐标轴的title和label
以下例子展示对colour
和shape
的改动。如果在legend中同时使用colour
和shape
,则它们都需要指定scale。否则会有两个两个独立的legend。
# A different data set
df1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
# Specify colour and shape
lp1 <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + geom_line() + geom_point()
lp1
# Here's what happens if you just specify colour
lp1 + scale_colour_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man"))
# Specify both colour and shape
lp1 + scale_colour_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man")) +
scale_shape_discrete(name ="Payer",
breaks=c("Female", "Male"),
labels=c("Woman", "Man"))
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 5.2. 修改原始数据
pg <- PlantGrowth # Copy data into new data frame
# Rename the column and the values in the factor
levels(pg$group)[levels(pg$group)=="ctrl"] <- "Control"
levels(pg$group)[levels(pg$group)=="trt1"] <- "Treatment 1"
levels(pg$group)[levels(pg$group)=="trt2"] <- "Treatment 2"
names(pg)[names(pg)=="group"] <- "Experimental Condition"
# View a few rows from the end product
head(pg)
#> weight Experimental Condition
#> 1 4.17 Control
#> 2 5.58 Control
#> 3 5.18 Control
#> 4 6.11 Control
#> 5 4.50 Control
#> 6 4.61 Control
# Make the plot
ggplot(data=pg, aes(x=`Experimental Condition`, y=weight, fill=`Experimental Condition`)) +
geom_boxplot()
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
legend 和 坐标轴的title,label 同时发生变动。
# 6. 修改图例标题和标签的外观
# Title appearance
bp + theme(legend.title = element_text(colour="blue", size=16, face="bold"))
# Label appearance
bp + theme(legend.text = element_text(colour="blue", size = 16, face = "bold"))
2
3
4
5
6
# 7. 修改图例外框
默认情况下,legend不显示外框,可通过theme来自定义。
bp + theme(legend.background = element_rect())
bp + theme(legend.background = element_rect(fill="#bbded6"))
# not show
# 添加外边框
bp + theme(
legend.background = element_rect(color = "blue",
linetype = "solid", size = 1)
)
bp + theme(
# Change legend background color
legend.background = element_rect(fill = "gray"),
legend.key = element_rect(fill = "yellow", color = NA),
# Change legend key size and key width
legend.key.size = unit(1.5, "cm"),
legend.key.width = unit(1,"cm")
)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 8. 改变图例的位置
bp + theme(legend.position="top")
# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right)
bp + theme(legend.position=c(.5, .5))
# Set the "anchoring point" of the legend (bottom-left is 0,0; top-right is 1,1)
# Put bottom-left corner of legend box in bottom-left corner of graph
bp + theme(legend.justification=c(0,0), legend.position=c(0,0))
# Put bottom-right corner of legend box in bottom-right corner of graph
bp + theme(legend.justification=c(1,0), legend.position=c(1,0))
2
3
4
5
6
7
8
9
10
11
# 9. 在图例中隐藏border
在绘制带有轮廓的条形图时color = "black"
,它将在图例中的颜色块绘制外边框。目前ggplot2没有内置的删除色块外边框的方法,可通过覆盖绘图的方式去掉这些外边框。
# No outline
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
geom_bar()
# Add outline, but slashes appear in legend
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
geom_bar(colour="black")
# A hack to hide the slashes: first graph the bars with no outline and add the legend,
# then graph the bars again with outline, but with a blank legend.
ggplot(data=PlantGrowth, aes(x=group, fill=group)) +
geom_bar() +
geom_bar(colour="black", show.legend=FALSE)
2
3
4
5
6
7
8
9
10
11
12
13
# 10. scale的种类
scale
有很多种。它们采用 scale_xxx_yyy
的形式。以下是xxx和yyy的一些常用值: