在 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