在 R 中使用 ggplot 建立自定義圖例

Jinku Hu 2023年1月30日 2021年7月14日
  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

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相關文章 - R Plot