HOWTO · R

R에서 ggplot을 사용하여 사용자 지정 범례 만들기

이 기사에서는 R에서 ggplot을 사용하여 사용자 정의 범례를 만드는 방법을 소개합니다.

이 기사에서는 R에서ggplot을 사용하여 사용자 정의 범례를 만드는 여러 방법을 보여줍니다.

theme함수에서legend.position매개 변수를 사용하여 R에서 범례 위치 지정

legend.position매개 변수는 플롯에서 범례 위치를 지정합니다. 선택적 값은"none","left","right","bottom","top"또는 요소를 2 개 가진 숫자 형 벡터 일 수 있습니다. 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