在 R 中使用 ggplot 创建自定义图例

Jinku Hu 2023年1月30日
  1. 使用 theme 函数中的 legend.position 参数指定 R 中的图例位置
  2. theme 函数中使用 legend.justificationlegend.background 参数来创建自定义图例
  3. 使用 theme 函数中的 legend.title 参数修改图例标题格式
在 R 中使用 ggplot 创建自定义图例

本文将演示在 R 中使用 ggplot 创建自定义图例的多种方法。

使用 theme 函数中的 legend.position 参数指定 R 中的图例位置

legend.position 参数指定图中的图例位置。可选值可以是 "none""left""right""bottom""top" 或二元素数值向量。plot.title 参数在以下示例中也用于修改绘图的标题。最后,使用 grid.arrange 函数同时绘制两个图。

library(ggplot2)
library(gridExtra)
library(babynames)
library(dplyr)

dat <- babynames %>%
  filter(name %in% c("Alice", "Maude", "Mae")) %>%
  filter(sex=="F")

p1 <- ggplot(dat, aes(x = year, y = n, color = name)) +
  geom_line() +
  scale_y_continuous(
    breaks = seq(0, 15000, 1000),
    name = "Number of babies") +
  ggtitle("Name Popularity Through Years")

p2 <- ggplot(dat, aes(x = year, y = n, color = name)) +
  geom_line() +
  scale_y_continuous(
    breaks = seq(0, 15000, 1000),
    name = "Number of babies") +
  theme(
    legend.position = "left",
    plot.title = element_text(
      size = rel(1.2), lineheight = .9,
      family = "Calibri", face = "bold", colour = "brown"
    )) +
  ggtitle("Name Popularity Through Years")

grid.arrange(p1, p2, nrow = 2)

ggplot 自定义图例 1

theme 函数中使用 legend.justificationlegend.background 参数来创建自定义图例

theme 函数的另一个有用参数是 legend.background,可用于设置图例背景的格式。下面的代码片段用白色和黑色描边填充图例矩形。此外,legend.justificationlegend.position 结合以指定图例的位置。

library(ggplot2)
library(gridExtra)
library(babynames)
library(dplyr)

dat <- babynames %>%
  filter(name %in% c("Alice", "Maude", "Mae")) %>%
  filter(sex=="F")

p3 <- ggplot(dat, aes(x = year, y = n, color = name)) +
  geom_line() +
  scale_y_continuous(
    breaks = seq(0, 15000, 1000),
    name = "Number of babies") +
  theme(
    legend.position = c(1, 1),
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = "white", colour = "black"),
    plot.title = element_text(
      size = rel(1.2), lineheight = .9,
      family = "Calibri", face = "bold", colour = "brown"
    )) +
  ggtitle("Name Popularity Through Years")


p4 <- ggplot(dat, aes(x = year, y = n, color = name)) +
  geom_line() +
  scale_color_discrete(limits = c("Maude", "Mae", "Alice")) +
  scale_y_continuous(
    breaks = seq(0, 15000, 1000),
    name = "Number of babies") +
  theme(
    legend.position = c(1, 1),
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = "white", colour = "black"),
    plot.title = element_text(
      size = rel(1.2), lineheight = .9,
      family = "Calibri", face = "bold", colour = "brown"
    )) +
  ggtitle("Name Popularity Through Years")

grid.arrange(p3, p4, nrow = 2)

ggplot 自定义图例 2

使用 theme 函数中的 legend.title 参数修改图例标题格式

legend.title 参数可用于更改图例标题格式。它需要带有不同参数的 element_text 函数来修改字体系列、文本颜色或字体大小等格式。grid.arrange 函数用于演示两个绘制图形之间的变化。

library(ggplot2)
library(gridExtra)
library(babynames)
library(dplyr)

dat <- babynames %>%
  filter(name %in% c("Alice", "Maude", "Mae")) %>%
  filter(sex=="F")

p5 <- ggplot(dat, aes(x = year, y = n, color = name)) +
  geom_line() +
  scale_color_discrete(limits = c("Maude", "Mae", "Alice")) +
  labs(color = "Name") +
  scale_y_continuous(
    breaks = seq(0, 15000, 1000),
    name = "Number of babies") +
  theme(
    legend.position = c(1, 1),
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = "white", colour = "black"),
    plot.title = element_text(
      size = rel(1.2), lineheight = .9,
      family = "Calibri", face = "bold", colour = "brown"
    )) +
  ggtitle("Name Popularity Through Years")


p6 <- ggplot(dat, aes(x = year, y = n, color = name)) +
  geom_line() +
  scale_color_discrete(limits = c("Maude", "Mae", "Alice")) +
  labs(color = "Name") +
  scale_y_continuous(
    breaks = seq(0, 15000, 1000),
    name = "Number of babies") +
  theme(
    legend.title = element_text(
      family = "Calibri",
      colour = "brown",
      face = "bold",
      size = 12),
    legend.position = c(1, 1),
    legend.justification = c(1, 1),
    legend.background = element_rect(fill = "white", colour = "black"),
    plot.title = element_text(
      size = rel(1.2), lineheight = .9,
      family = "Calibri", face = "bold", colour = "brown"
    )) +
  ggtitle("Name Popularity Through Years")

grid.arrange(p5, p6, nrow = 2)

ggplot 自定义图例 3

作者: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

DelftStack.com 创始人。Jinku 在机器人和汽车行业工作了8多年。他在自动测试、远程测试及从耐久性测试中创建报告时磨练了自己的编程技能。他拥有电气/电子工程背景,但他也扩展了自己的兴趣到嵌入式电子、嵌入式编程以及前端和后端编程。

LinkedIn Facebook

相关文章 - R Plot