Guides图例与增加坐标轴
图例函数:
-
guide_colorbar()/guide_colourbar()用于连续变量的图例 -
guide_legend()用于离散变量的图例,也可以用于连续变量 -
guides()将_colorbar和_legend嵌套进去,方便映射,如guides(fill = guide_colorbar())
可以在scale_xxx()标度中指定guide类型,guide = "colorbar"或guide = “legend”
常用公共参数:
| 作用对象 | 参数 | 描述 |
|---|---|---|
| 整个图例 | direction | 指定图例箱体排列方向,"horizontal"水平排列,或"vertical"垂直排列 |
| reverse | 逻辑值,是否翻转图例顺序,默认从小到大自上而下,翻转后从小到大自下而上 | |
| order | 为数字,表示给图例编号,方便多个图例排列 | |
| 图例标题 | title | 指定标题名称 |
| title.position | 标题相对图例箱体的位置, 水平图例为"left"或"right", 垂直图例为"top"或"bottom" | |
| title.hjust | 为数字,指定图例标题水平位置偏移 | |
| title.vjust | 为数字,指定图例标题垂直位置偏移 | |
| 图例刻度标签 | label | 为逻辑值,是否显示图例刻度标签 |
| label.position | 指定图例刻度标签相对箱体位置, 水平图例为"left"或"right", 垂直图例为"top"或"bottom" | |
| label.hjust | 为数字,指定图例刻度标签水平位置偏移 | |
| label.vjust | 为数字,指定图例刻度标签垂直位置偏移 | |
| default.unit | 表示指定箱体尺寸单位,用grid::unit()
|
guide_colorbar
**_colorbar()参数: **
| 作用对象 | 参数 | 描述 |
|---|---|---|
| 图例箱体 | barwidth | 指定箱体宽度,为数字或grid::unit()指定,默认单位为mm |
| barheight | 指定箱体高度,为数字或grid::unit()指定 |
|
| nbin | 指定分箱数,数字越大则渐变约平缓 | |
| raster | 逻辑值,表示是否将图例以删格形式呈现,不常用,栅格数据 | |
| 箱体边框 | frame.colour | 表示指定箱体边框颜色,默认无边框 |
| frame.linetype | 表示指定箱体边框线型 | |
| frame.linewidth | 表示指定箱体边框线宽 | |
| 刻度线 | ticks | 逻辑值,表示是否显示刻度线 |
| ticks.colour | 指定刻度线颜色 | |
| ticks.linewidth | 指定刻度线线宽 | |
| draw.ulim | 逻辑值,表示是否显示最大值(upper)刻度线 | |
| draw.llim | 逻辑值,表示是否显示最小值(low)刻度线 |
library(ggplot2)
library(reshape2)
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p2 <- p1 + geom_point(aes(size = value))
p1 + scale_fill_continuous(guide = "colorbar") # 默认形式
p1 + guides(fill = guide_colorbar()) # 具体映射
p1 + scale_fill_distiller(palette = "YlGn", direction = 1) +
guides(fill = guide_colorbar(title = "值", nbin = 100, # 指定图例名称,水平放置,增加分箱数为100
barwidth = 0.5, barheight = 10,# 指定图例箱体尺寸,宽为0.5mm,高为10mm
ticks.colour = "red", # 更改刻度线颜色
frame.colour = "blue",frame.linewidth = 0.5, # 增加箱体边框
draw.ulim = TRUE, draw.llim = TRUE # 显示最大,最小刻度线
))
p2 + scale_fill_continuous(guide = "colorbar") + scale_size(guide = "legend") # 在标度中控制图例
p2 + guides(fill = "colorbar", size = "legend") # 与上面结果一样
p2 + scale_fill_continuous(guide = guide_colorbar(direction = "horizontal")) +
scale_size(guide = guide_legend(direction = "vertical")) # 更改图例方向
guide_legend
_legend()参数:
| 作用对象 | 参数 | 描述 |
|---|---|---|
| 箱体尺寸 | key.width | 指定单个箱体宽度, 为数字或grid::unit()指定, 默认单位为mm |
| key.height | 指定单个箱体高度, 为数字或grid::unit()指定 |
|
| 分箱排列 | nrow | 为数字,表示指定图例箱体排列行数 |
| ncol | 为数字,表示指定图例箱体排列列数 | |
| byrow | 逻辑值,表示图例箱体是否按行排列,默认FALSE按列排 |
library(ggplot2)
library(reshape2)
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p2 <- p1 + geom_point(aes(size = value))
p1 + scale_fill_continuous(guide = guide_legend()) # 连续标度中设置离散图例
p1 + scale_fill_distiller(type = "qual", palette = "Set3") +
guides(fill = guide_legend(title = "左", title.position = "left", # 指定图例名称为"左", 位置为箱体的左边
key.width = 5, key.height = 10, nrow = 2, ncol = 2, byrow = TRUE # 修改箱体尺寸,并矩形排列,按行排
))
p1 + guides(fill = guide_legend(
title.theme = element_text(size = 15, face = "italic", colour = "red", angle = 0)) # 在图例中修改图例主题,一般在主题函数内修改
)
p1 + scale_fill_continuous(breaks = c(5, 10, 15),
labels = paste("long", c(5, 10, 15)),
guide = guide_legend(
direction = "horizontal", # 水平排列箱体
title.position = "top", # 图例标题置于顶部
label.position = "bottom", # 图例刻度标签置于底部
label.hjust = 0.5, # 刻度标签水平位置偏移
label.vjust = 1, # 刻度标签垂直位置偏移
label.theme = element_text(angle = 90) # 图例主题中修改刻度标签角度
)
)














网友评论