ggplot 图例(你想要的都在这了!)
感谢老俊俊的大力支持。我们会每日跟新,欢迎您关注老俊俊的生信笔记。

点击上方关注我们
简言
关于 ggplot 的图例,说到调整也无非是 位置
和 名称
的调整居多,剩下的一般用到的很少,不过 ggplot
是包含很多关于图例的调整的函数,结合我的理解和经验,给大家详细介绍一下关于图例的调整。
1、geom_xx 图层
在每个图层里都有 show.legend
参数来是否显示图例(如果有映射的话):
# 默认绘图
p1 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot()
# 去除图例
p2 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot(show.legend = F)
p1 | p2

也可以通过 theme
主题函数的 legend.position 和 guides
函数去除:
# theme函数
p3 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = 'none')
# guides函数
p4 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
guides(fill = 'none')
p3 | p4

2、theme 函数的图例大解析
theme 函数里含有很多的图例函数,方便细致的调整图例:
legend.background, # 图例背景
legend.margin, # 图例边缘
legend.spacing, # 图例单元之间的间隔
legend.spacing.x, # 图例单元之间的水平间隔
legend.spacing.y, # 图例单元之间的竖直间隔
legend.key, # 图例符号背景
legend.key.size, # 图例符号大小
legend.key.height, # 图例符号高度
legend.key.width, # 图例符号宽度
legend.text, # 图例文字
legend.text.align, # 图例文字对齐方式
legend.title, # 图例标题
legend.title.align, # 图例标题对齐方式
legend.position, # 图例位置
legend.direction, # 图例方向
legend.justification, # 图例对齐调整
legend.box, # 多个图例的排列,水平或竖直
legend.box.just, # 多个图例的对齐调整
legend.box.margin, # 多个图例边缘距离
legend.box.background, # 多个图例背景
legend.box.spacing, # 多个图例与图之间的间距
下面演示具体的例子:
图例背景和图距:
# legend.background, # 图例背景
p5 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.background = element_rect(colour = 'green',fill = 'pink'))
# legend.margin, # 图例边缘
p6 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.margin = margin(2,2,2,2,'cm'))
p5 | p6

图例间距:
# legend.spacing.x, # 图例单元之间的水平间隔
p7 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = 'top',
legend.spacing.x = unit(3,'cm'))
# legend.spacing.y, # 图例之间的竖直间隔
p8 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = 'right',
legend.spacing.y = unit(3,'cm'))
p7 / p8

图例符号大小:
# legend.key, # 图例符号背景
p9 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.key = element_rect(colour = 'red',fill = 'orange'))
# legend.key.size, # 图例符号大小
p10 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.key.size = unit(2,'cm'))
# legend.key.height, # 图例符号高度
# legend.key.width, # 图例符号宽度
p11 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.key.height = unit(2,'cm'),
legend.key.width = unit(0.5,'cm'))
p9 | (p10 / p11)

图例标签:
# legend.text, # 图例文字
p12 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.text = element_text(colour = 'blue',face = 'bold.italic'))
# legend.text.align, # 图例文字对齐方式
p13 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.text = element_text(colour = 'blue',face = 'bold.italic'),
legend.text.align = 1)
p12 | p13

图例标题:
# legend.title, # 图例标题
p14 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.title = element_text(colour = 'orange',face = 'bold'))
# legend.title.align, # 图例标题对齐方式
p15 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.title = element_text(colour = 'orange',face = 'bold'),
legend.title.align = 1)
p14 | p15

图例位置:
# legend.position, # 图例位置
p16 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = 'left')
p17 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = c(0.7,0.6))
# legend.direction, # 图例方向
p18 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = c(0.5,0.8),
legend.direction = 'horizontal')
# legend.justification, # 图例对齐调整
p19 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = c(1,1),
legend.justification = 'center')
p20 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species)) +
geom_boxplot() +
theme(legend.position = c(1,1),
legend.justification = c(1,1))
(p16 |p17 | p18)/(p19 | p20)

多图例排列:
# legend.box, # 多个图例的排列,水平或竖直
p21 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species,color = Sepal.Width)) +
geom_jitter(position = position_jitter(width = 0.2)) +
theme(legend.box = 'vertical')
p22 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species,color = Sepal.Width)) +
geom_jitter(position = position_jitter(width = 0.2)) +
theme(legend.box = 'horizontal')
# legend.box.just, # 多个图例的对齐调整
p23 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species,color = Sepal.Width)) +
geom_jitter(position = position_jitter(width = 0.2)) +
theme(legend.box = 'horizontal',
legend.box.just = 'bottom')
p21 / (p22 | p23)

多图例背景及间距:
# legend.box.margin, # 多个图例边缘距离
p24 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species,color = Sepal.Width)) +
geom_jitter(position = position_jitter(width = 0.2)) +
theme(legend.box.margin = margin(2,2,2,2,'cm'))
# legend.box.background, # 多个图例背景
p25 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species,color = Sepal.Width)) +
geom_jitter(position = position_jitter(width = 0.2)) +
theme(legend.box.background = element_rect(colour = 'black',fill = 'purple'))
# legend.box.spacing, # 多个图例与图之间的间距
p26 <- ggplot(iris,aes(x = Species,y = Sepal.Length,fill = Species,color = Sepal.Width)) +
geom_jitter(position = position_jitter(width = 0.2)) +
theme(legend.box.spacing = unit(2,'cm'))
p24 / (p25 | p26)

3、guides 指南函数调整图例
我们主要了解 guide_colourbar
和 guide_legend
函数,前者针对于 连续型 数据,后者应用于 离散型 图例。前面我们可以在 guides
里设置 fill/color = 'none'
来隐藏图例。
我们先看看 guide_colourbar 函数:
guide_colorbar(
title = waiver(), # 图例标题
title.position = NULL, # 图例标题位置
title.theme = NULL, # 图例标题样式,可用element.text调整
title.hjust = NULL, # 图例的水平对齐
title.vjust = NULL, # 图例的竖直对齐
label = TRUE, # 图例的符号文字标签
label.position = NULL, # 图例的符号文字标签位置
label.theme = NULL, # 图例的符号文字标签式,可用element.text调整
label.hjust = NULL, # 文字标签水平对齐
label.vjust = NULL, # 文字标签竖直对齐
barwidth = NULL, # 条形的宽度
barheight = NULL, # 条形的高度
nbin = 300, # 色阶数量
raster = TRUE, # 是否渲染为光栅对象,默认是
frame.colour = NULL, # 条形边框颜色
frame.linewidth = 0.5, # 条形边框线粗
frame.linetype = 1, # 条形边框线型
ticks = TRUE, # 是否显示刻度线
ticks.colour = "white", # 刻度线颜色
ticks.linewidth = 0.5, # 刻度线宽度
draw.ulim = TRUE, # 上边的刻度标记是否显示
draw.llim = TRUE, # 下边的刻度标记是否显示
direction = NULL, # 图例水平或者竖直
default.unit = "line", # 条形高度和宽度的单位,字符串向量
reverse = FALSE, # 图例是否逆序排列
order = 0, # 多个图例的排列顺序
available_aes = c("colour", "color", "fill"), # 映射变量,字符串
...
)
使用示例,默认绘图:
# 构造测试数据
df <- data.frame(a = LETTERS[1:10],b = rnorm(10),z = rnorm(10),x = letters[1:10])
p1 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point()
p1

改变图例标题:
p2 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(title = 'JunJUN',
title.position = 'left',
title.theme =
element_text(size = 14,
color = 'red',
face = 'bold'),
title.hjust = 0.5,
title.vjust = 0.5))
p1 | p2

图例标签:
p2 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(label = F))
p3 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(label.position = 'left',
label.theme = element_text(color = 'orange',
face = 'bold.italic'),
label.hjust = 1,
label.vjust = 0.5))
p1 | p2 | p3

图例高度、宽度和色阶:
p4 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(barwidth = 1,barheight = 15))
p5 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(barwidth = 1,barheight = 15,
nbin = 3))
p1 | p4 | p5

图例边框:
p6 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(frame.colour = 'black',
frame.linewidth = 2,
frame.linetype = 'dashed'))
p6

图例刻度、图例方向:
p7 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(ticks = F))
p8 <- ggplot(df,aes(x = b,y = z,color = b)) +
geom_point() +
guides(color = guide_colorbar(ticks.colour = 'red',
ticks.linewidth = 4,
direction = 'horizontal'))
p7 | p8

图例刻度逆序:
p9 <- ggplot(df,aes(x = b,y = z,color = b,)) +
geom_point() +
guides(color = guide_colorbar(reverse = T))
p9

我们再看看 guide_legend
函数,其实很多参数是差不多的:
guide_legend(
title = waiver(), # 同上
title.position = NULL, # 同上
title.theme = NULL, # 同上
title.hjust = NULL, # 同上
title.vjust = NULL, # 同上
label = TRUE, # 同上
label.position = NULL, # 同上
label.theme = NULL, # 同上
label.hjust = NULL, # 同上
label.vjust = NULL, # 同上
keywidth = NULL, # 符号宽度
keyheight = NULL, # 符号高度
direction = NULL, # 同上
default.unit = "line", # 同上
override.aes = list(),
nrow = NULL, # 图例分成几行
ncol = NULL, # 图例分成几列
byrow = FALSE, # 是否水平
reverse = FALSE, # 同上
order = 0, # 同上
...
)
符号高度宽度,水平:
p10 <- ggplot(df[1:6,],aes(x = b,y = z,color = a)) +
geom_point(size = 5)
p11 <- ggplot(df[1:6,],aes(x = b,y = z,color = a)) +
geom_point(size = 5) +
guides(color = guide_legend(keywidth = unit(1,'cm'),
keyheight = unit(2,'cm'),
direction = 'horizontal'))
p10 | p11

分行列数:
p11 <- ggplot(df[1:6,],aes(x = b,y = z,color = a)) +
geom_point(size = 5) +
guides(color = guide_legend(ncol = 2))
p12 <- ggplot(df[1:6,],aes(x = b,y = z,color = a)) +
geom_point(size = 5) +
guides(color = guide_legend(nrow = 4))
p11 | p12

多个图例的位置调整:
p13 <- ggplot(df[1:6,],aes(x = b,y = z,color = a,shape = x)) +
geom_point(size = 5)
p14 <- ggplot(df[1:6,],aes(x = b,y = z,color = a,shape = x)) +
geom_point(size = 5) +
guides(shape = guide_legend(order = 1),
color = guide_legend(order = 2))
p13 | p14

# 图例排序
ggplot(mpg, aes(displ, cty)) +
geom_point(aes(size = hwy, colour = cyl, shape = drv)) +
guides(
colour = guide_colourbar(order = 1),
shape = guide_legend(order = 2),
size = guide_legend(order = 3))

4、连续性和离散型图例分别控制
如果我们一个图里既有 离散型图例 ,又有 连续型图例 ,我们分别调整即可:
# 离散和连续图例分别调整
p15 <- ggplot(df[1:6,],aes(x = b,y = z,color = b,shape = x)) +
geom_point(size = 5)
p16 <- ggplot(df[1:6,],aes(x = b,y = z,color = b,shape = x)) +
geom_point(size = 5) +
guides(color = guide_colorbar(title = 'continues',
label.theme = element_text(angle = 90,
face = 'bold',
colour = 'red')),
shape = guide_legend(title = 'discrete',
label.position = 'left',
nrow = 2,
keywidth = unit(1,'cm')))
p15 | p16

5、通过对应标度函数调整
我们也可以通过scale_XX_XX
对应的函数来修改图例:
ggplot(df[1:6,],aes(x = b,y = z,color = b,shape = x)) +
geom_point(size = 5) +
scale_color_gradient(low = 'green',high = 'red',
guide = guide_colorbar(title = 'continues',
label.theme =
element_text(angle = 90,
face = 'bold',
colour = 'red'))) +
scale_shape_discrete(guide = guide_legend(title = 'discrete',
label.position = 'left',
nrow = 2,
keywidth = unit(1,'cm')))

好了,差不多了,后面有机会再讲其它的,眼睛要瞎掉了,哈哈。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群
哦,代码已上传至QQ群文件夹。
群二维码:

老俊俊微信:
知识星球:
所以今天你学习了吗?
欢迎小伙伴留言评论!
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀ComplexHeatmap 之 Legends 续(二)
◀ComplexHeatmap 之 Legends 续(一)
◀ComplexHeatmap 之 Heatmap List 续(二)
◀ComplexHeatmap 之 Heatmap List 续(一)
◀ComplexHeatmap 之 Heatmap List
◀ComplexHeatmap 之 Heatmap Annotations 续(三)
◀…
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!