최고의 ggplot2 테마에 대한 완벽한 가이드


이 튜토리얼은 다음을 포함하여 최고의 ggplot2 테마에 대한 완전한 가이드를 제공합니다.

  • 내장된 ggplot2 테마를 사용하여 플롯의 모양을 변경하는 방법.
  • ggthemes 라이브러리에서 사전 정의된 테마를 사용하여 플롯의 모양을 변경하는 방법.
  • 경로 패널 배경 및 격자선을 포함하여 테마의 특정 구성 요소를 편집하는 방법.

내장된 ggplot2 테마를 사용하여 플롯 모양을 변경하는 방법

다음 각 예에서는 내장된 R 데이터 세트의 붓꽃을 사용합니다.

 #view first six rows of iris dataset
head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa

먼저 ggplot2 라이브러리를 로드하고 X축에 Sepal.Length를 표시 하고 종에 따라 색상이 지정된 Sepal.Width를 y축에 표시하는 산점도를 만듭니다.

 #load ggplot2 library
library(ggplot2)

#create scatterplot
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point()

다음으로 내장된 각 ggplot2 테마가 플롯의 모양에 어떤 영향을 미치는지 보여 드리겠습니다.

회색_테마

회색 배경과 흰색 격자가 있는 기본 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_gray()

bw_테마

흰색 바탕에 검은색 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_bw()

theme_linedraw

흰색 배경에 굵기가 다른 검은색 선만 있는 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_linedraw()

테마_빛

theme_linedraw 와 유사하지만 데이터에 더 많은 관심을 끌도록 설계된 회색 선과 축이 있는 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_light()

dark_theme

theme_light 와 유사하지만 배경이 어두운 테마입니다. 미세한 색상의 선을 표현하는데 유용한 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_dark()

최소 테마

배경 주석이 없는 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_minimal()

classic_theme

그리드가 없는 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_classic()

theme_void

완전히 비어 있는 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_void()

ggthemes 라이브러리에서 미리 정의된 테마를 사용하여 플롯의 모양을 변경하는 방법

내장된 ggplot2 테마를 사용하는 것 외에도 ggthemes 라이브러리에서 미리 정의된 테마를 사용하여 플롯의 미학을 변경할 수 있습니다.

먼저 ggthemes 라이브러리를 로드합니다.

 library(ggthemes)

그런 다음 미리 정의된 테마를 사용하여 플롯의 미학을 수정하는 몇 가지 예를 보여 드리겠습니다.

theme_wsj

월스트리트저널의 주제.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_wsj()

theme_tufte

통계학자인 Edward Tufte의 작업에서 영감을 받은 미니멀리스트 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_tufte()

Solarized_theme

Solarized Palette를 기반으로 색상을 사용한 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_solarized()

light = FALSE 인수를 사용하여 플롯에서 어두운 배경을 사용할 수도 있습니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_solarized( light = FALSE )

theme_gdocs

Google Docs 차트 기본값이 포함된 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_gdocs()

theme_fivethirtyeight

fivethirtyeight.com 의 플롯에서 영감을 받은 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_fivethirtyeight()

테마_경제학자

The Economist에서 영감을 받은 테마입니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_economist()

플롯의 특정 구성 요소를 편집하는 방법

theme()element_ect() 함수를 사용하여 플롯 패널의 배경색을 변경할 수 있습니다.

 theme(panel.background = element_rect(fill, color, size))
  • 채우기: 직사각형의 채우기 색상
  • 색상: 테두리 색상
  • 크기: 테두리 크기

element_line() 함수를 사용하여 그리드의 크기와 모양을 변경할 수도 있습니다.

 theme(panel.grid.major = element_line(color, size, linetype),
      panel.grid.minor = element_line(color, size, linetype))
  • 색상: 테두리 색상
  • 크기: 테두리 크기
  • linetype: 선종류(“공백”, “실선”, “대시”, “점선”, “점 대시”, “긴 대시”, “대시 2개”)

다음 코드는 플롯 패널 테두리와 눈금선을 제거하는 방법을 보여줍니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank())

다음 코드는 플롯 패널 배경과 눈금선을 변경하는 방법을 보여줍니다.

 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(
    panel.background = element_rect(fill = "powderblue",
    color = "powderblue",
    size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid', color = "white"),
    panel.grid.minor = element_line(size = 0.25, linetype = 'solid', color = "white")
  )

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다