图例类型指南显示映射到值的键(即几何图形)。如果可能的话,集成各种比例的图例指南。
用法
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,
  ...
)
参数
- title
 - 
指示指南标题的字符串或表达式。如果是
NULL,则不显示标题。默认情况下 (waiver()),比例对象的名称或labs()中指定的名称用作标题。 - title.position
 - 
表示标题位置的字符串。 "top"(垂直参考线默认)、"bottom"、"left"(水平参考线默认)或 "right." 之一
 - title.theme
 - 
用于渲染标题文本的主题对象。通常需要
element_text()对象。默认情况下,主题由theme()或主题中的legend.title指定。 - title.hjust
 - 
指定标题文本的水平对齐方式的数字。
 - title.vjust
 - 
指定标题文本垂直对齐的数字。
 - label
 - 
合乎逻辑的。如果
TRUE则绘制标签。如果FALSE则标签不可见。 - label.position
 - 
指示标签位置的字符串。 "top"、"bottom"(水平参考线默认)、"left" 或 "right"(垂直参考线默认)之一。
 - label.theme
 - 
用于渲染标签文本的主题对象。通常需要
element_text()对象。默认情况下,主题由theme()中的legend.text指定。 - label.hjust
 - 
指定标签文本水平对齐的数字。对于表达式,标准文本的默认值是 0(左对齐)和 1(右对齐)。
 - label.vjust
 - 
指定标签文本垂直对齐的数字。
 - keywidth
 - 
指定图例键宽度的数字或
grid::unit()对象。默认值为theme()中的legend.key.width或legend.key.size。 - keyheight
 - 
指定图例键高度的数字或
grid::unit()对象。默认值为theme()中的legend.key.height或legend.key.size。 - direction
 - 
指示引导方向的字符串。 "horizontal" 或 "vertical." 之一
 - default.unit
 - 
对于
keywidth和keyheight表示grid::unit()的字符串。 - override.aes
 - 
指定图例键的美观参数的列表。请参阅详细信息和示例。
 - nrow
 - 
所需的图例行数。
 - ncol
 - 
所需的图例列数。
 - byrow
 - 
合乎逻辑的。如果
FALSE(默认),则legend-matrix按列填充,否则legend-matrix按行填充。 - reverse
 - 
合乎逻辑的。如果
TRUE,则图例的顺序相反。 - order
 - 
小于 99 的正整数,指定该指南在多个指南中的顺序。这控制多个指南的显示顺序,而不是指南本身的内容。如果为 0(默认),则顺序由秘密算法确定。
 - ...
 - 
被忽略。
 
细节
可以在每个 scale_* 或 guides() 中指定指南。 scale_* 中的 guide = "legend" 是 guide = guide_legend() 的语法糖(例如 scale_color_manual(guide = "legend") )。至于如何更详细地指定每个尺度的指南,请参见guides()。
也可以看看
其他指南:guide_bins()、guide_colourbar()、guide_coloursteps()、guides()
例子
# \donttest{
df <- expand.grid(X1 = 1:10, X2 = 1:10)
df$value <- df$X1 * df$X2
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p2 <- p1 + geom_point(aes(size = value))
# Basic form
p1 + scale_fill_continuous(guide = guide_legend())
# Control styles
# title position
p1 + guides(fill = guide_legend(title = "LEFT", title.position = "left"))
# title text styles via element_text
p1 + guides(fill =
  guide_legend(
    title.theme = element_text(
      size = 15,
      face = "italic",
      colour = "red",
      angle = 0
    )
  )
)
# label position
p1 + guides(fill = guide_legend(label.position = "left", label.hjust = 1))
# label styles
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)
    )
  )
# Set aesthetic of legend key
# very low alpha value make it difficult to see legend key
p3 <- ggplot(mtcars, aes(vs, am, colour = factor(cyl))) +
  geom_jitter(alpha = 1/5, width = 0.01, height = 0.01)
p3
# override.aes overwrites the alpha
p3 + guides(colour = guide_legend(override.aes = list(alpha = 1)))
# multiple row/col legends
df <- data.frame(x = 1:20, y = 1:20, color = letters[1:20])
p <- ggplot(df, aes(x, y)) +
  geom_point(aes(colour = color))
p + guides(col = guide_legend(nrow = 8))
p + guides(col = guide_legend(ncol = 8))
p + guides(col = guide_legend(nrow = 8, byrow = TRUE))
# reversed order legend
p + guides(col = guide_legend(reverse = TRUE))
# }
相关用法
- R ggplot2 guide_bins Guide_legend 的分箱版本
 - R ggplot2 guide_axis 轴导轨
 - R ggplot2 guide_coloursteps 离散颜色条指南
 - R ggplot2 guide_colourbar 连续色条指南
 - R ggplot2 guides 为每个尺度设置指南
 - R ggplot2 geom_qq 分位数-分位数图
 - R ggplot2 geom_spoke 由位置、方向和距离参数化的线段
 - R ggplot2 geom_quantile 分位数回归
 - R ggplot2 geom_text 文本
 - R ggplot2 get_alt_text 从绘图中提取替代文本
 - R ggplot2 geom_ribbon 函数区和面积图
 - R ggplot2 geom_boxplot 盒须图(Tukey 风格)
 - R ggplot2 geom_hex 二维箱计数的六边形热图
 - R ggplot2 geom_bar 条形图
 - R ggplot2 geom_bin_2d 二维 bin 计数热图
 - R ggplot2 ggplot 创建一个新的ggplot
 - R ggplot2 geom_jitter 抖动点
 - R ggplot2 geom_point 积分
 - R ggplot2 geom_linerange 垂直间隔:线、横线和误差线
 - R ggplot2 ggsf 可视化 sf 对象
 - R ggplot2 geom_blank 什么也不画
 - R ggplot2 ggsave 使用合理的默认值保存 ggplot (或其他网格对象)
 - R ggplot2 ggtheme 完整的主题
 - R ggplot2 geom_path 连接观察结果
 - R ggplot2 geom_violin 小提琴情节
 
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Legend guide。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
