Ggplot2 का उपयोग करके r में आसानी से एक राहत प्लॉट कैसे बनाएं


बम्प चार्ट एक प्रकार का चार्ट है जो परिवर्तन की मात्रा के बजाय समूहों के क्रम पर जोर देने के लिए निरपेक्ष मूल्यों के बजाय समय के साथ विभिन्न समूहों की रैंकिंग दिखाता है।

यह ट्यूटोरियल बताता है कि ggplot2 का उपयोग करके आसानी से R में बम्प प्लॉट कैसे बनाया जाए।

उदाहरण: एक राहत ग्राफ़िक बनाना

R में बम्प चार्ट बनाने के लिए, हमें पहले दो पैकेज लोड करने होंगे: dplyr और ggplot2 :

 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-दिन की अवधि में पांच अलग-अलग टीमों की “रैंकिंग” दिखाता है।

हम इस अवधि के दौरान प्रत्येक दिन प्रत्येक टीम की रैंकिंग देखने के लिए प्रगति चार्ट बनाने के लिए 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))

यह बम्प चार्ट आपके इच्छित प्रारूप में डेटा प्रदर्शित करता है, लेकिन यह बहुत बदसूरत है। कुछ सौंदर्यात्मक बदलावों के साथ हम इस पेंटिंग को और बेहतर बना सकते हैं।

बम्प ग्राफ़िक को स्टाइलाइज़ करें

चार्ट की उपस्थिति को बेहतर बनाने के लिए, हम डोमिनिक कोच द्वारा बनाई गई निम्नलिखित थीम का उपयोग कर सकते हैं:

 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()

हम स्केल_कलर_मैनुअल() तर्क जोड़कर पंक्तियों में से किसी एक को आसानी से हाइलाइट कर सकते हैं। उदाहरण के लिए, निम्नलिखित कोड में, हम टीम ए की लाइन को बैंगनी और अन्य सभी लाइनों को ग्रे बनाते हैं:

 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'))

एक टिप्पणी जोड़ने

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *