- 改 图例背景颜色
- 改 图例大小
- 改 图例符号颜色
第一部分
1.1 图例背景颜色

1.2 代码和语法
base + theme(
legend.background = element_rect(
fill = "lightblue", # 填充色
colour = "black", # 框线色
size = 1.5 ) ) # 线条宽度
语法
- 在theme主题系统中修改,元素名为legend.background,函数名element_rect()
- 参数fill = “” 指填充色,colour = “” 指框线色
第二部分
2.1 图例的大小

2.2 代码和语法
base + theme(
legend.key.size = unit(35, "pt")
# 或
legend.key.height = unit(35, "pt"),
legend.key.width = unit(55, "pt")
)
解释
- 图例大小,由图例名,图例符号,标签名共同决定。我目前还不知道哪个函数可以直接改图例大小。
- 图例符号是图中红色框出来的部分,这部分对整个图例大小的影响最大。所以用图例符号大小来决定图例大小。上面的代码改的是图例符号。
语法
- theme主题系统,元素名是legend.key.size / height / width ,函数是unit(),这是一个通用函数,类似margin()。
- size、height、width 区别,size改整体,整体放大或缩小,不改变宽高比。height / width改高度和宽度,相应宽高比会改变。当size设定后,无法改height / width。
- unit(35, “pt”)设定大小时, 第一个参数是数值,第二个参数是单位,一般选"pt",因为好调,还可选"mm"、“cm”、“inches”。调到合适的大小,需要自己多试几次。
第三部分
3.1 图例符号颜色

3.2 代码和语法
base + theme(
legend.key = element_rect(
color = "red", # 框线色
fill = "lemonchiffon")) # 填充色
语法
- theme主题系统,元素名是legend.key(图例符号) ,函数是element_rect() 。
- 图例符号的叫法参考书籍《ggplot2数据分析与图形艺术(第二版)》