R တွင် လူဦးရေပိရမစ်ကို ဖန်တီးနည်း
လူဦးရေပိရမစ် သည် ပေးထားသောလူဦးရေ၏ အသက်နှင့် လိင်ခွဲဝေမှုကို ပြသသည့် ဂရပ်တစ်ခုဖြစ်သည်။ ဤသည်မှာ လူဦးရေ၏ဖွဲ့စည်းမှုနှင့် လက်ရှိလူဦးရေတိုးနှုန်းကို အလွယ်တကူနားလည်ရန် အသုံးဝင်သောဇယားဖြစ်သည်။
လူဦးရေပိရမစ်တွင် စတုဂံပုံသဏ္ဍာန်ရှိပါက၊ လူဦးရေသည် နှေးကွေးသောနှုန်းဖြင့် ကြီးထွားနေကြောင်း ညွှန်ပြပါသည်။ အနီးစပ်ဆုံး အရွယ်တူ မျိုးဆက်သစ်များဖြင့် အစားထိုးသည်။
လူဦးရေပိရမစ်သည် ပိရမစ်တစ်ခုကဲ့သို့ ပုံသဏ္ဍာန်ဖြစ်ပါက၊ လူဦးရေသည် ပိုမိုမြန်ဆန်စွာ ကြီးထွားလာနေကြောင်း ညွှန်ပြပါသည်။ မျိုးဆက်ဟောင်းများသည် မျိုးဆက်သစ်၊ ကြီးမားသော မျိုးဆက်များကို မွေးထုတ်ပေးသည်။
ဂရပ်တွင် ကျား၊မကို ဘယ်ညာနှင့် ဘယ်ဘက်ခြမ်းတွင် ပြထားပြီး၊ အသက်ကို y ဝင်ရိုးပေါ်တွင် ပြထားပြီး လူဦးရေ ရာခိုင်နှုန်း သို့မဟုတ် ပမာဏကို x-axis တွင် ပြထားသည်။
ဤသင်ခန်းစာတွင် R တွင် လူဦးရေပိရမစ်တစ်ခုဖန်တီးနည်းကို ရှင်းပြထားသည်။
R တွင် လူဦးရေပိရမစ်တစ်ခုကို ဖန်တီးပါ။
ကျွန်ုပ်တို့တွင် အသက် (0 မှ 100 နှစ်) နှင့် လိင် (M = “ Male” , F = “ Female” ) တို့ကို အခြေခံ၍ လူဦးရေ၏ ရာခိုင်နှုန်းဖွဲ့စည်းပုံကို ပြသသည့် အောက်ပါဒေတာအစုံရှိသည်ဆိုပါစို့။
#make this example reproducible set.seed(1) #create data frame data <- data.frame(age = rep(1:100, 2), gender = rep(c("M", "F"), each = 100)) #add variable population data$population <- 1/sqrt(data$age) * runif(200, 10000, 15000) #convert population variable to percentage data$population <- data$population / sum(data$population) * 100 #view first six rows of dataset head(data) # age gender population #1 1M 2.424362 #2 2M 1.794957 #3 3M 1.589594 #4 4M 1.556063 #5 5M 1.053662 #6 6M 1.266231
ggplot2 စာကြည့်တိုက်ကို အသုံးပြု၍ ဤဒေတာအတွဲအတွက် အခြေခံလူဦးရေပိရမစ်တစ်ခုကို ကျွန်ုပ်တို့ ဖန်တီးနိုင်သည်-
#load ggplot2 library(ggplot2) #create population pyramid ggplot(data, aes(x = age, fill = gender, y = ifelse(test = gender == "M", yes = -population, no = population))) + geom_bar(stat = "identity") + scale_y_continuous(labels = abs, limits = max(data$population) * c(-1,1)) + coordinate_flip()
ခေါင်းစဉ်များနှင့် တဂ်များထည့်ခြင်း။
labs() အကြောင်းပြချက်ကို အသုံးပြု၍ လူဦးရေပိရမစ်တွင် ခေါင်းစဉ်များနှင့် ဝင်ရိုးအညွှန်းများ နှစ်ခုလုံးကို ထည့်နိုင်သည်-
ggplot(data, aes(x = age, fill = gender,
y = ifelse(test = gender == "M",
yes = -population, no = population))) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = abs, limits = max(data$population) * c(-1,1)) +
labs(title = "Population Pyramid", x = "Age", y = "Percent of population") +
coordinate_flip()
အရောင်ပြောင်းပါ။
scale_color_manual() အငြင်းအခုံကို အသုံးပြု၍ လိင်များကို ကိုယ်စားပြုရန် အသုံးပြုသည့် အရောင်နှစ်မျိုးကို ကျွန်ုပ်တို့ ပြောင်းလဲနိုင်သည်-
ggplot(data, aes(x = age, fill = gender, y = ifelse(test = gender == "M", yes = -population, no = population))) + geom_bar(stat = "identity") + scale_y_continuous(labels = abs, limits = max(data$population) * c(-1,1)) + labs(title = "Population Pyramid", x = "Age", y = "Percent of population") + scale_color_manual(values = c("pink", "steelblue"), aesthetics = c("color", "fill")) + coordinate_flip()
ခေတ်ပေါင်းများစွာ ပိရမစ်များ
facet_wrap() အငြင်းအခုံကို အသုံးပြု၍ လူဦးရေပိရမစ်အများအပြားကို တွဲဆွဲရန်လည်း ဖြစ်နိုင်သည်။ ဥပမာအားဖြင့်၊ ကျွန်ုပ်တို့တွင် နိုင်ငံ A၊ B နှင့် C အတွက် လူဦးရေဒေတာ ရှိသည်ဆိုပါစို့။ အောက်ဖော်ပြပါ ကုဒ်သည် နိုင်ငံတစ်ခုစီအတွက် လူဦးရေပိရမစ်တစ်ခု ဖန်တီးပုံကို သရုပ်ဖော်သည်-
#make this example reproducible set.seed(1) #create data frame data_multiple <- data.frame(age = rep(1:100, 6), gender = rep(c("M", "F"), each = 300), country = rep(c("A", "B", "C"), each = 100, times = 2)) #add variable population data_multiple$population <- round(1/sqrt(data_multiple$age)*runif(200, 10000, 15000), 0) #view first six rows of dataset head(data_multiple) # age gender country population #1 1 MA 11328 #2 2 MA 8387 #3 3 MA 7427 #4 4 MA 7271 #5 5 MA 4923 #6 6 MA 5916 #create one population pyramid per country ggplot(data_multiple, aes(x = age, fill = gender, y = ifelse(test = gender == "M", yes = -population, no = population))) + geom_bar(stat = "identity") + scale_y_continuous(labels = abs, limits = max(data_multiple$population) * c(-1,1)) + labs(y = "Population Amount") + coordinate_flip() + facet_wrap(~country) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) #rotate x-axis labels
အပြင်အဆင်ကို ပြောင်းပါ။
နောက်ဆုံးတွင်၊ ဂရပ်ဖစ်၏အခင်းအကျင်းကိုပြောင်းလဲနိုင်သည်။ ဥပမာအားဖြင့်၊ အောက်ဖော်ပြပါကုဒ်သည် ဂရပ်ဖစ်ကို ပိုမိုသေးငယ်သောပုံဖြစ်အောင်ပြုလုပ်ရန် theme_classic() ကိုအသုံးပြုသည်-
ggplot(data_multiple, aes(x = age, fill = gender, y = ifelse(test = gender == "M", yes = -population, no = population))) + geom_bar(stat = "identity") + scale_y_continuous(labels = abs, limits = max(data_multiple$population) * c(-1,1)) + labs(y = "Population Amount") + coordinate_flip() + facet_wrap(~country) + theme_classic() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
သို့မဟုတ် စိတ်ကြိုက် ggthemes ကိုသုံးနိုင်သည်။ ggthemes ၏စာရင်းအပြည့်အစုံအတွက်၊ စာရွက်စာတမ်းစာမျက်နှာကို ကြည့်ပါ ။