Ggplot2를 사용하여 r에서 구호 플롯을 쉽게 만드는 방법


범프 차트는 변화량보다는 그룹의 순서를 강조하기 위해 절대값 대신 시간에 따른 다양한 그룹의 순위를 보여주는 차트의 일종이다.

이 튜토리얼에서는 ggplot2를 사용하여 R에서 범프 플롯을 쉽게 생성하는 방법을 설명합니다.

예: 릴리프 그래픽 만들기

R에서 범프 차트를 만들려면 먼저 dplyrggplot2 라는 두 개의 패키지를 로드해야 합니다.

 library(ggplot2) #for creating bump chart
library(dplyr) #for manipulating data

다음으로 작업할 몇 가지 데이터를 만듭니다.

 #set the seed to make this example reproducible
set.seed(10)

data <- data.frame(team = rep(LETTERS[1:5], each = 10),
                   random_num = runif(50),
                   day = rep(1:10, 5))

data <- data %>%
  group_by(day) %>%
  arrange(day, desc(random_num), team) %>% 
  mutate(rank = row_number()) %>%
  A group()

head(data)

# team random_num day rank          
#1 C 0.865 1 1
#2 B 0.652 1 2
#3 D 0.536 1 3
#4 A 0.507 1 4
#5 E 0.275 1 5
#6 C 0.615 2 1

이 데이터베이스는 단순히 10일 동안 5개 팀의 “순위”를 표시합니다.

ggplot2를 사용하여 이 기간 동안 매일 각 팀의 순위를 시각화하는 진행 차트를 만들 수 있습니다.

 ggplot(data, aes(x = day, y = rank, group = team)) +
  geom_line(aes(color = team, alpha = 1), size = 2) +
  geom_point(aes(color = team, alpha = 1), size = 4) +
  scale_y_reverse(breaks = 1:nrow(data))

이 범프 차트는 원하는 형식으로 데이터를 표시하지만 꽤 보기 흉합니다. 몇 가지 미학적 변화를 통해 이 그림을 훨씬 더 좋게 만들 수 있습니다.

범프 그래픽 스타일 지정

차트의 모양을 개선하기 위해 Dominik Koch 가 만든 다음 테마를 사용할 수 있습니다.

 my_theme <- function() {

  # Colors
  color.background = "white"
  color.text = "#22211d"

  # Begin construction of chart
  theme_bw(base_size=15) +

    # Format background colors
    theme(panel.background = element_rect(fill=color.background,
                                          color=color.background)) +
    theme(plot.background = element_rect(fill=color.background,
                                          color=color.background)) +
    theme(panel.border = element_rect(color=color.background)) +
    theme(strip.background = element_rect(fill=color.background,
                                          color=color.background)) +

    # Format the grid
    theme(panel.grid.major.y = element_blank()) +
    theme(panel.grid.minor.y = element_blank()) +
    theme(axis.ticks = element_blank()) +

    # Format the legend
    theme(legend.position = "none") +

    # Format title and axis labels
    theme(plot.title = element_text(color=color.text, size=20, face = "bold")) +
    theme(axis.title.x = element_text(size=14, color="black", face = "bold")) +
    theme(axis.title.y = element_text(size=14, color="black", face = "bold",
                                          vjust=1.25)) +
    theme(axis.text.x = element_text(size=10, vjust=0.5, hjust=0.5,
                                          color = color.text)) +
    theme(axis.text.y = element_text(size=10, color = color.text)) +
    theme(strip.text = element_text(face = "bold")) +

    # Plot margins
    theme(plot.margin = unit(c(0.35, 0.2, 0.3, 0.35), "cm"))
}

범프 차트를 다시 만들지만 이번에는 범례를 제거하고 일부 차트 레이블을 추가하고 위 코드에 정의된 테마를 사용하겠습니다.

 ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +
  geom_line(aes(color = team, alpha = 1), size = 2) +
  geom_point(aes(color = team, alpha = 1), size = 4) +
  geom_point(color = "#FFFFFF", size = 1) +
  scale_y_reverse(breaks = 1:nrow(data)) + 
  scale_x_discrete(breaks = 1:10) +
  theme(legend.position = 'none') +
  geom_text(data = data %>% filter(day == "1"),
            aes(label = team, x = 0.5), hjust = .5,
            fontface = "bold", color = "#888888", size = 4) +
  geom_text(data = data %>% filter(day == "10"),
            aes(label = team, x = 10.5), hjust = 0.5,
            fontface = "bold", color = "#888888", size = 4) +
  labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +
  my_theme()

scale_color_manual() 인수를 추가하여 행 중 하나를 쉽게 강조 표시할 수도 있습니다. 예를 들어 다음 코드에서는 A 팀의 라인을 보라색으로 만들고 다른 모든 라인을 회색으로 만듭니다.

 ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +
  geom_line(aes(color = team, alpha = 1), size = 2) +
  geom_point(aes(color = team, alpha = 1), size = 4) +
  geom_point(color = "#FFFFFF", size = 1) +
  scale_y_reverse(breaks = 1:nrow(data)) + 
  scale_x_discrete(breaks = 1:10) +
  theme(legend.position = 'none') +
  geom_text(data = data %>% filter(day == "1"),
            aes(label = team, x = 0.5), hjust = .5,
            fontface = "bold", color = "#888888", size = 4) +
  geom_text(data = data %>% filter(day == "10"),
            aes(label = team, x = 10.5), hjust = 0.5,
            fontface = "bold", color = "#888888", size = 4) +
  labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +
  my_theme() +
  scale_color_manual(values = c('purple', 'grey', 'grey', 'grey', 'grey'))

원하는 경우 여러 줄을 강조 표시할 수도 있습니다.

 ggplot(data, aes(x = as.factor(day), y = rank, group = team)) +
  geom_line(aes(color = team, alpha = 1), size = 2) +
  geom_point(aes(color = team, alpha = 1), size = 4) +
  geom_point(color = "#FFFFFF", size = 1) +
  scale_y_reverse(breaks = 1:nrow(data)) + 
  scale_x_discrete(breaks = 1:10) +
  theme(legend.position = 'none') +
  geom_text(data = data %>% filter(day == "1"),
            aes(label = team, x = 0.5), hjust = .5,
            fontface = "bold", color = "#888888", size = 4) +
  geom_text(data = data %>% filter(day == "10"),
            aes(label = team, x = 10.5), hjust = 0.5,
            fontface = "bold", color = "#888888", size = 4) +
  labs(x = 'Day', y = 'Rank', title = 'Team Ranking by Day') +
  my_theme() +
  scale_color_manual(values = c('purple', 'steelblue', 'grey', 'grey', 'grey'))

의견을 추가하다

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