R에서 인구 피라미드를 만드는 방법
인구 피라미드는 특정 인구의 연령과 성별 분포를 보여주는 그래프입니다. 인구 구성과 현재 인구 증가 추세를 쉽게 이해할 수 있는 유용한 차트입니다.
인구 피라미드가 직사각형 모양이라면 인구가 더 느린 속도로 증가하고 있음을 나타냅니다. 이전 세대는 거의 같은 크기의 새로운 세대로 대체됩니다.
인구 피라미드가 피라미드 모양이라면 인구가 더 빠른 속도로 증가하고 있음을 나타냅니다. 더 오래된 세대는 새롭고 더 큰 세대를 낳습니다.
그래프의 왼쪽과 오른쪽에는 성별이 표시되고, y축에는 연령이 표시되며, x축에는 인구의 비율이나 양이 표시됩니다.
이 튜토리얼에서는 R에서 인구 피라미드를 만드는 방법을 설명합니다.
R에서 인구 피라미드 만들기
연령(0~100세) 및 성별(M = “남성”, F = “여성”)을 기준으로 인구 구성 비율을 보여주는 다음 데이터 세트가 있다고 가정합니다.
#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의 전체 목록을 보려면 설명서 페이지를 참조하세요 .