目录
1、不显示图例
R
theme(legend.position = "none")
引号内可选参数有right、left、bottom,对应右、下、左
2、自定义图例位置
R
theme(legend.position=c(0, 1),
legend.justification=c(0, 1))
元素 legend.position 和 legend.justification 同时出现并且值相同,想象图例在坐标轴里,则c(0, 1)为面板左上角,c(1, 0)为面板右下角,c(0.5, 0,5)为面板中间,面板指绘图区域。
3、修改图例背景颜色、外框颜色、大小
R
theme(
legend.background = element_rect(
fill = "lightblue", # 填充色
colour = "black", # 框线色
size = 1.5 ) ) # 线条宽度
4、修改图例大小
R
theme(
# legend.key.size = unit(35, "pt") # 统一修改
# 或
legend.key.height = unit(35, "pt"),
legend.key.width = unit(55, "pt")
)
5、图例设置背景、线框为空
R
theme(legend.background = element_blank(), # 图例的背景被隐藏
legend.box.background = element_blank()) # 图例的边框被隐藏
6、自定义设置多个图例的标题
R
guides(size=guide_legend(title="A"), # 数值变量的
#fill=guide_legend(title="A"), # 填充变量的
col=guide_colorbar(title ="a")) # 颜色变量的
7、设置多个图例的之间的间隔
R
theme(
legend.margin = margin(20, unit = "pt") # 设置图例之间的间隔
)
8、取消不需要的图例显示
将不需要显示的图例类型设置为:"none" 即可
R
guides(size=guide_legend(title="A"),
col ="none")