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))
ဤအဖုအထစ်ဇယားသည် သင်လိုချင်သည့်ပုံစံဖြင့် ဒေတာကိုပြသပေးသော်လည်း ၎င်းသည် အလွန်ရုပ်ဆိုးပါသည်။ အလှအပဆိုင်ရာ ပြောင်းလဲမှု အနည်းငယ်ဖြင့် ဤပန်းချီကားကို များစွာ ပိုမိုကောင်းမွန်အောင် ပြုလုပ်နိုင်သည်။
အဖုအထစ် ဂရပ်ဖစ်ကို ပုံစံထုတ်ပါ။
ဇယား၏အသွင်အပြင်ကို မြှင့်တင်ရန်အတွက် 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() အကြောင်းပြချက်ကို ထည့်ခြင်းဖြင့် အတန်းများထဲမှ တစ်ခုကို အလွယ်တကူ မီးမောင်းထိုးပြနိုင်သည်။ ဥပမာအားဖြင့်၊ အောက်ပါကုဒ်တွင်၊ ကျွန်ုပ်တို့သည် Team 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'))