install.packages(“foreign”)
install.packages(“haven”)
install.packages(“dplyr”)
library(foreign)
library(haven)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(readxl)
raw_welfare<-read_sav("https://www.dropbox.com/s/m2s7p3c5bvhvf5o/Koweps_hpc10_2015_beta1.sav?dl=1")
raw_welfare -> welfare
head(welfare)
## # A tibble: 6 x 957
## h10_id h10_ind h10_sn h10_merkey h_new h10_cobf h10_reg5 h10_reg7 h10_din
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 1 10101 0 NA 1 1 864
## 2 2 1 1 20101 0 NA 1 1 600
## 3 3 1 1 30101 0 NA 1 1 1571
## 4 4 1 1 40101 0 NA 1 1 3579
## 5 4 1 1 40101 0 NA 1 1 3579
## 6 6 1 1 60101 0 NA 1 1 3030
## # ... with 948 more variables: h10_cin <dbl>, h10_flag <dbl>, p10_wgl <dbl>,
## # p10_wsl <dbl>, p10_wgc <dbl>, p10_wsc <dbl>, h10_hc <dbl>, nh1001_1 <dbl>,
## # nh1001_2 <dbl>, h1001_1 <dbl>, h10_pind <dbl>, h10_pid <dbl>, h10_g1 <dbl>,
## # h10_g2 <dbl>, h10_g3 <dbl>, h10_g4 <dbl>, h10_g6 <dbl>, h10_g7 <dbl>,
## # h10_g8 <dbl>, h10_g9 <dbl>, h10_g10 <dbl>, h10_g11 <dbl>, h10_g12 <dbl>,
## # h1001_110 <dbl>, h1001_5aq1 <dbl>, h1001_5aq2 <dbl>, h1001_5aq3 <dbl>,
## # h1001_5aq4 <dbl>, h10_med1 <dbl>, h10_med2 <dbl>, h10_med3 <dbl>,
## # h10_med4 <dbl>, h10_med5 <dbl>, h10_med6 <dbl>, h10_med7 <dbl>,
## # h10_med8 <dbl>, h10_g9_1 <dbl>, h10_med9 <dbl>, h10_med10 <dbl>,
## # h10_eco1 <dbl>, h10_eco2 <dbl>, h10_eco3 <dbl>, h10_eco4 <dbl>,
## # h10_eco4_1 <dbl>, h10_eco5_1 <dbl>, h10_eco6 <dbl>, h10_eco_7_1 <dbl>,
## # h10_eco_7_2 <dbl>, h10_eco_7_3 <dbl>, h10_eco8 <dbl>, h10_eco9 <dbl>,
## # h10_eco10 <dbl>, h10_eco11 <dbl>, h10_soc1 <dbl>, h10_soc_2 <dbl>,
## # h10_soc_3 <dbl>, h10_soc_4 <dbl>, h10_soc_5 <dbl>, h10_soc_6 <dbl>,
## # h10_soc_7 <dbl>, h10_soc_8 <dbl>, h10_soc_9 <dbl>, h10_soc_10 <dbl>,
## # h10_soc_11 <dbl>, h10_soc8 <dbl>, h10_soc9 <dbl>, h10_soc11 <dbl>,
## # h10_soc10 <dbl>, h10_soc_12 <dbl>, h10_soc_13 <dbl>, h1005_1 <dbl>,
## # h1005_3aq1 <dbl>, h1005_2 <dbl>, h1005_3 <dbl>, h1005_4 <dbl>,
## # h1005_5 <dbl>, h1005_6 <dbl>, h1005_7 <dbl>, nh1005_8 <dbl>,
## # nh1005_9 <dbl>, h1005_3aq2 <dbl>, h1006_aq1 <dbl>, h1006_1 <dbl>,
## # h1006_2 <dbl>, h1006_4 <dbl>, h1006_5 <dbl>, h1006_3 <dbl>, h1006_6 <dbl>,
## # h1006_8 <dbl>, h1006_9 <dbl>, h1006_aq2 <dbl>, h1006_aq3 <dbl>,
## # h1006_10 <dbl>, h1006_11 <dbl>, h1006_12 <dbl>, h1006_13 <dbl>,
## # h1006_14 <dbl>, h1006_21 <dbl>, h1006_22 <dbl>, h1006_23 <dbl>, ...
welfare <- dplyr::rename(welfare,
sex = h10_g3,
birth = h10_g4,
marriage = h10_g10,
religion = h10_g11,
income = p1002_8aq1,
code_job = h10_eco9,
code_region = h10_reg7)
library(dplyr)
class(welfare$sex)
## [1] "numeric"
table(welfare$sex)
##
## 1 2
## 7578 9086
welfare$sex <- ifelse(welfare$sex == 9, NA, welfare$sex)
table(is.na(welfare$sex))
##
## FALSE
## 16664
welfare$sex <- ifelse(welfare$sex == 1, "male", "female")
table(welfare$sex)
##
## female male
## 9086 7578
qplot(welfare$sex)
library(dplyr)
class(welfare$income)
## [1] "numeric"
summary(welfare$income)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.0 122.0 192.5 241.6 316.6 2400.0 12030
qplot(welfare$income) + xlim(0, 1000)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 12051 rows containing non-finite values (stat_bin).
## Warning: Removed 2 rows containing missing values (geom_bar).
welfare$income <- ifelse(welfare$income == 0 | welfare$income ==9999, NA, welfare$income)
table(is.na(welfare$income))
##
## FALSE TRUE
## 4620 12044
sex_income <- welfare %>%
filter(!is.na(income)) %>%
group_by(sex) %>%
summarise(mean_income = mean(income))
## `summarise()` ungrouping output (override with `.groups` argument)
sex_income
## # A tibble: 2 x 2
## sex mean_income
## <chr> <dbl>
## 1 female 163.
## 2 male 312.
ggplot(data = sex_income, aes(x = sex, y = mean_income)) + geom_col()
library(dplyr)
class(welfare$birth)
## [1] "numeric"
summary(welfare$birth)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1907 1946 1966 1968 1988 2014
qplot(welfare$birth)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
table(is.na(welfare$birth))
##
## FALSE
## 16664
welfare$birth <- ifelse(welfare$birth == 9999, Na, welfare$birth)
table(is.na(welfare$birth))
##
## FALSE
## 16664
welfare$age <- 2015 - welfare$birth + 1
summary(welfare$age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.00 28.00 50.00 48.43 70.00 109.00
qplot(welfare$age)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
library(dplyr)
age_income <- welfare %>%
filter(!is.na(income)) %>%
group_by(age) %>%
summarise(mean_income = mean(income))
## `summarise()` ungrouping output (override with `.groups` argument)
head(age_income)
## # A tibble: 6 x 2
## age mean_income
## <dbl> <dbl>
## 1 20 121.
## 2 21 106.
## 3 22 130.
## 4 23 142.
## 5 24 134.
## 6 25 145.
ggplot(data = age_income, aes(x = age, y = mean_income)) + geom_line()
welfare <- welfare %>%
mutate(ageg = ifelse(age < 30, "young",
ifelse(age <= 59, "middle", "old")))
table(welfare$ageg)
##
## middle old young
## 6049 6281 4334
qplot(welfare$ageg)
library(dplyr)
ageg_income <- welfare %>%
filter(!is.na(income)) %>%
group_by(ageg) %>%
summarise(mean_income = mean(income))
## `summarise()` ungrouping output (override with `.groups` argument)
ageg_income
## # A tibble: 3 x 2
## ageg mean_income
## <chr> <dbl>
## 1 middle 282.
## 2 old 125.
## 3 young 164.
ggplot(data = ageg_income, aes(x = ageg, y = mean_income)) + geom_col()
ggplot(data = ageg_income, aes(x = ageg, y = mean_income)) + geom_col() + scale_x_discrete(limits = c("young", "middle", "old"))
sex_income <- welfare %>%
filter(!is.na(income)) %>%
group_by(ageg, sex) %>%
summarise(mean_income = mean(income))
## `summarise()` regrouping output by 'ageg' (override with `.groups` argument)
sex_income
## # A tibble: 6 x 3
## # Groups: ageg [3]
## ageg sex mean_income
## <chr> <chr> <dbl>
## 1 middle female 188.
## 2 middle male 353.
## 3 old female 81.5
## 4 old male 174.
## 5 young female 160.
## 6 young male 171.
ggplot(data = sex_income, aes(x = ageg, y = mean_income)) + geom_col() + scale_x_discrete(limits = c("young", "middle", "old"))
ggplot(data = sex_income, aes(x = ageg, y = mean_income, fill = sex)) + geom_col(position = "dodge") + scale_x_discrete(limits = c("young", "middle", "old"))
sex_age <- welfare %>%
filter(!is.na(income)) %>%
group_by(age, sex) %>%
summarise(mean_income = mean(income))
## `summarise()` regrouping output by 'age' (override with `.groups` argument)
head(sex_age)
## # A tibble: 6 x 3
## # Groups: age [3]
## age sex mean_income
## <dbl> <chr> <dbl>
## 1 20 female 147.
## 2 20 male 69
## 3 21 female 107.
## 4 21 male 102.
## 5 22 female 140.
## 6 22 male 118.
ggplot(data = sex_age, aes(x = age, y = mean_income, col = sex)) + geom_line()
class(welfare$code_job)
## [1] "numeric"
table(welfare$code_job)
##
## 111 120 131 132 133 134 135 139 141 149 151 152 153 159 211 212
## 2 16 10 11 9 3 7 10 35 20 26 18 15 16 8 4
## 213 221 222 223 224 231 232 233 234 235 236 237 239 241 242 243
## 3 17 31 12 4 41 5 3 6 48 14 2 29 12 4 63
## 244 245 246 247 248 251 252 253 254 259 261 271 272 273 274 281
## 4 33 59 77 38 14 111 24 67 109 4 15 11 4 36 17
## 283 284 285 286 289 311 312 313 314 320 330 391 392 399 411 412
## 8 10 26 16 5 140 260 220 84 75 15 4 13 87 47 12
## 421 422 423 429 431 432 441 442 510 521 522 530 611 612 613 620
## 124 71 5 14 20 33 154 197 192 353 5 106 1320 11 40 2
## 630 710 721 722 730 741 742 743 751 752 753 761 762 771 772 773
## 20 29 30 22 16 27 3 34 34 5 49 69 27 11 61 86
## 774 780 791 792 799 811 812 819 821 822 823 831 832 841 842 843
## 7 17 5 21 45 16 1 6 9 9 23 5 17 32 10 4
## 851 852 853 854 855 861 862 863 864 871 873 874 875 876 881 882
## 19 13 7 33 9 3 14 17 31 2 257 34 37 2 2 3
## 891 892 899 910 921 922 930 941 942 951 952 953 991 992 999 1011
## 8 19 16 102 31 74 289 325 99 125 122 73 45 12 141 2
## 1012
## 17
library(readxl)
list_job <- read_excel("koweps_codebook.xlsx", col_names = T, sheet = 2)
head(list_job)
## # A tibble: 6 x 2
## code_job job
## <dbl> <chr>
## 1 111 의회의원 고위공무원 및 공공단체임원
## 2 112 기업고위임원
## 3 120 행정 및 경영지원 관리자
## 4 131 연구 교육 및 법률 관련 관리자
## 5 132 보험 및 금융 관리자
## 6 133 보건 및 사회복지 관련 관리자
dim(list_job)
## [1] 149 2
welfare <- left_join(welfare, list_job, id = "code_job")
## Joining, by = "code_job"
welfare %>% filter(!is.na(code_job)) %>%
select(code_job, job) %>%
head(10)
## # A tibble: 10 x 2
## code_job job
## <dbl> <chr>
## 1 942 경비원 및 검표원
## 2 762 전기공
## 3 530 방문 노점 및 통신 판매 관련 종사자
## 4 999 기타 서비스관련 단순 종사원
## 5 312 경영관련 사무원
## 6 254 문리 기술 및 예능 강사
## 7 510 영업 종사자
## 8 530 방문 노점 및 통신 판매 관련 종사자
## 9 286 스포츠 및 레크레이션 관련 전문가
## 10 521 매장 판매 종사자
library(foreign)
library(haven)
library(dplyr)
library(ggplot2)
job_income <- welfare %>%
filter(!is.na(job) & !is.na(income)) %>%
group_by(job) %>%
summarise(mean_income = mean(income))
## `summarise()` ungrouping output (override with `.groups` argument)
head(job_income)
## # A tibble: 6 x 2
## job mean_income
## <chr> <dbl>
## 1 가사 및 육아 도우미 80.2
## 2 간호사 241.
## 3 건설 및 광업 단순 종사원 190.
## 4 건설 및 채굴 기계운전원 358.
## 5 건설 전기 및 생산 관련 관리자 536.
## 6 건설관련 기능 종사자 247.
top10 <- job_income %>%
arrange(desc(mean_income)) %>%
head(10)
top10
## # A tibble: 10 x 2
## job mean_income
## <chr> <dbl>
## 1 금속 재료 공학 기술자 및 시험원 845.
## 2 의료진료 전문가 844.
## 3 의회의원 고위공무원 및 공공단체임원 750
## 4 보험 및 금융 관리자 726.
## 5 제관원 및 판금원 572.
## 6 행정 및 경영지원 관리자 564.
## 7 문화 예술 디자인 및 영상 관련 관리자 557.
## 8 연구 교육 및 법률 관련 관리자 550.
## 9 건설 전기 및 생산 관련 관리자 536.
## 10 석유 및 화학물 가공장치 조작원 532.
ggplot(data = top10, aes(x = reorder(job, mean_income), y = mean_income)) + geom_col() + coord_flip()
bot10 <- job_income %>%
arrange(mean_income) %>%
head(10)
bot10
## # A tibble: 10 x 2
## job mean_income
## <chr> <dbl>
## 1 가사 및 육아 도우미 80.2
## 2 임업관련 종사자 83.3
## 3 기타 서비스관련 단순 종사원 88.2
## 4 청소원 및 환경 미화원 88.8
## 5 약사 및 한약사 89
## 6 작물재배 종사자 92
## 7 농립어업관련 단순 종사원 102.
## 8 의료 복지 관련 서비스 종사자 104.
## 9 음식관련 단순 종사원 108.
## 10 판매관련 단순 종사원 117.
ggplot(data = bot10, aes(x = reorder(job, -mean_income), y = mean_income)) + geom_col() + coord_flip()
ggplot(data = bot10, aes(x = reorder(job, -mean_income), y = mean_income)) + geom_col() + coord_flip() + ylim(0, 850)
library(dplyr)
job_male <- welfare %>%
filter(!is.na(job) & sex == "male") %>%
group_by(job) %>%
summarise(n = n()) %>%
arrange(desc(n)) %>%
head(10)
## `summarise()` ungrouping output (override with `.groups` argument)
job_male
## # A tibble: 10 x 2
## job n
## <chr> <int>
## 1 작물재배 종사자 640
## 2 자동차 운전원 251
## 3 경영관련 사무원 213
## 4 영업 종사자 141
## 5 매장 판매 종사자 132
## 6 제조관련 단순 종사원 104
## 7 청소원 및 환경 미화원 97
## 8 건설 및 광업 단순 종사원 95
## 9 경비원 및 검표원 95
## 10 행정 사무원 92
job_female <- welfare %>%
filter(!is.na(job) & sex == "female") %>%
group_by(job) %>%
summarise(n = n()) %>%
arrange(desc(n)) %>%
head(10)
## `summarise()` ungrouping output (override with `.groups` argument)
job_female
## # A tibble: 10 x 2
## job n
## <chr> <int>
## 1 작물재배 종사자 680
## 2 청소원 및 환경 미화원 228
## 3 매장 판매 종사자 221
## 4 제조관련 단순 종사원 185
## 5 회계 및 경리 사무원 176
## 6 음식서비스 종사자 149
## 7 주방장 및 조리사 126
## 8 가사 및 육아 도우미 125
## 9 의료 복지 관련 서비스 종사자 121
## 10 음식관련 단순 종사원 104
ggplot(data = job_male, aes(x = reorder(job, n), y = n)) + geom_col() + coord_flip()
ggplot(data = job_female, aes(x = reorder(job, n), y = n)) + geom_col() + coord_flip()
class(welfare$religion)
## [1] "numeric"
table(welfare$religion)
##
## 1 2
## 8047 8617
welfare$religion <- ifelse(welfare$religion == 1, "yes", "no")
table(welfare$religion)
##
## no yes
## 8617 8047
qplot(welfare$religion)
library(dplyr)
class(welfare$marriage)
## [1] "numeric"
table(welfare$marriage)
##
## 0 1 2 3 4 5 6
## 2861 8431 2117 712 84 2433 26
welfare$group_marriage <- ifelse(welfare$marriage == 1, "marriage",
ifelse(welfare$marriage == 3, "divorce", NA))
table(welfare$marriage)
##
## 0 1 2 3 4 5 6
## 2861 8431 2117 712 84 2433 26
table(is.na(welfare$group_marriage))
##
## FALSE TRUE
## 9143 7521
qplot(welfare$group_marriage)
library(dplyr)
library(ggplot2)
religion_marriage <- welfare %>%
filter(!is.na(group_marriage)) %>%
group_by(religion, group_marriage) %>%
summarise(n = n()) %>%
mutate(tot_group = sum(n)) %>%
mutate(pct = round(n/tot_group*100, 1))
## `summarise()` regrouping output by 'religion' (override with `.groups` argument)
religion_marriage
## # A tibble: 4 x 5
## # Groups: religion [2]
## religion group_marriage n tot_group pct
## <chr> <chr> <int> <int> <dbl>
## 1 no divorce 384 4602 8.3
## 2 no marriage 4218 4602 91.7
## 3 yes divorce 328 4541 7.2
## 4 yes marriage 4213 4541 92.8
divorce <- religion_marriage %>%
filter(group_marriage == "divorce") %>%
select(religion, pct)
divorce
## # A tibble: 2 x 2
## # Groups: religion [2]
## religion pct
## <chr> <dbl>
## 1 no 8.3
## 2 yes 7.2
ggplot(data = divorce, aes(x = religion, y = pct)) + geom_col()
ageg_marriage <- welfare %>%
filter(!is.na(group_marriage)) %>%
group_by(ageg, group_marriage) %>%
summarise(n = n()) %>%
mutate(tot_group = sum(n)) %>%
mutate(pct = round(n/tot_group*100, 1))
## `summarise()` regrouping output by 'ageg' (override with `.groups` argument)
ageg_marriage
## # A tibble: 6 x 5
## # Groups: ageg [3]
## ageg group_marriage n tot_group pct
## <chr> <chr> <int> <int> <dbl>
## 1 middle divorce 437 4918 8.9
## 2 middle marriage 4481 4918 91.1
## 3 old divorce 273 4165 6.6
## 4 old marriage 3892 4165 93.4
## 5 young divorce 2 60 3.3
## 6 young marriage 58 60 96.7
ageg_marriage <- ageg_marriage %>%
filter(ageg != "young" & group_marriage == "divorce") %>%
select(ageg, pct)
ageg_marriage
## # A tibble: 2 x 2
## # Groups: ageg [2]
## ageg pct
## <chr> <dbl>
## 1 middle 8.9
## 2 old 6.6
ggplot(data = ageg_marriage, aes(x = ageg, y = pct)) + geom_col()
ageg_religion_marriage <- welfare %>%
filter(!is.na(group_marriage) & ageg != "young") %>%
group_by(ageg, religion, group_marriage) %>%
summarise(n = n()) %>%
mutate(tot_group = sum(n)) %>%
mutate(pct = round(n/tot_group*100, 1))
## `summarise()` regrouping output by 'ageg', 'religion' (override with `.groups` argument)
ageg_religion_marriage
## # A tibble: 8 x 6
## # Groups: ageg, religion [4]
## ageg religion group_marriage n tot_group pct
## <chr> <chr> <chr> <int> <int> <dbl>
## 1 middle no divorce 260 2681 9.7
## 2 middle no marriage 2421 2681 90.3
## 3 middle yes divorce 177 2237 7.9
## 4 middle yes marriage 2060 2237 92.1
## 5 old no divorce 123 1884 6.5
## 6 old no marriage 1761 1884 93.5
## 7 old yes divorce 150 2281 6.6
## 8 old yes marriage 2131 2281 93.4
df_divorce <- ageg_religion_marriage %>%
filter(group_marriage == "divorce") %>%
select(ageg, religion, pct)
df_divorce
## # A tibble: 4 x 3
## # Groups: ageg, religion [4]
## ageg religion pct
## <chr> <chr> <dbl>
## 1 middle no 9.7
## 2 middle yes 7.9
## 3 old no 6.5
## 4 old yes 6.6
ggplot(data = df_divorce, aes(x = ageg, y = pct, fill = religion)) + geom_col(position = "dodge")
class(welfare$code_region)
## [1] "numeric"
table(welfare$code_region)
##
## 1 2 3 4 5 6 7
## 2486 3711 2785 2036 1467 1257 2922
list_region <- data.frame(code_region = c(1:7),
region = c("서울",
"수도권(인천.경기)",
"부산/경남/울산",
"대구/경북",
"대전/충남",
"강원/충북",
"광주/전남/전북/제주도"))
list_region
## code_region region
## 1 1 서울
## 2 2 수도권(인천.경기)
## 3 3 부산/경남/울산
## 4 4 대구/경북
## 5 5 대전/충남
## 6 6 강원/충북
## 7 7 광주/전남/전북/제주도
welfare <- left_join(welfare, list_region, id = "code_region")
## Joining, by = "code_region"
welfare %>% select(code_region, region) %>%
head
## # A tibble: 6 x 2
## code_region region
## <dbl> <fct>
## 1 1 서울
## 2 1 서울
## 3 1 서울
## 4 1 서울
## 5 1 서울
## 6 1 서울
region_ageg <- welfare %>%
group_by(region, ageg) %>%
summarise(n = n()) %>%
mutate(tot_group = sum(n)) %>%
mutate(pct = round(n/tot_group*100, 2))
## `summarise()` regrouping output by 'region' (override with `.groups` argument)
head(region_ageg)
## # A tibble: 6 x 5
## # Groups: region [2]
## region ageg n tot_group pct
## <fct> <chr> <int> <int> <dbl>
## 1 강원/충북 middle 417 1257 33.2
## 2 강원/충북 old 555 1257 44.2
## 3 강원/충북 young 285 1257 22.7
## 4 광주/전남/전북/제주도 middle 947 2922 32.4
## 5 광주/전남/전북/제주도 old 1233 2922 42.2
## 6 광주/전남/전북/제주도 young 742 2922 25.4
ggplot(data = region_ageg, aes(x = region, y = pct, fill = ageg)) + geom_col() + coord_flip()
library(dplyr)
library(ggplot2)
list_order_old <- region_ageg %>%
filter(ageg == "old") %>%
arrange(pct)
list_order_old
## # A tibble: 7 x 5
## # Groups: region [7]
## region ageg n tot_group pct
## <fct> <chr> <int> <int> <dbl>
## 1 수도권(인천.경기) old 1109 3711 29.9
## 2 서울 old 805 2486 32.4
## 3 대전/충남 old 527 1467 35.9
## 4 부산/경남/울산 old 1124 2785 40.4
## 5 광주/전남/전북/제주도 old 1233 2922 42.2
## 6 강원/충북 old 555 1257 44.2
## 7 대구/경북 old 928 2036 45.6
list_order_old -> order
order
## # A tibble: 7 x 5
## # Groups: region [7]
## region ageg n tot_group pct
## <fct> <chr> <int> <int> <dbl>
## 1 수도권(인천.경기) old 1109 3711 29.9
## 2 서울 old 805 2486 32.4
## 3 대전/충남 old 527 1467 35.9
## 4 부산/경남/울산 old 1124 2785 40.4
## 5 광주/전남/전북/제주도 old 1233 2922 42.2
## 6 강원/충북 old 555 1257 44.2
## 7 대구/경북 old 928 2036 45.6
ggplot(data = region_ageg, aes(x = region, y = pct, fill = ageg)) +
geom_col() +
coord_flip()+
scale_x_discrete(limits = order)
## Warning: Continuous limits supplied to discrete scale.
## Did you mean `limits = factor(...)` or `scale_*_continuous()`?
## Warning: Removed 21 rows containing missing values (position_stack).
## Warning in rep(yes, length.out = len): 'x' is NULL so the result will be NULL
## Error in ans[ypos] <- rep(yes, length.out = len)[ypos]: replacement has length zero
library(dplyr)
library(ggplot2)
class(region_ageg$ageg)
## [1] "character"
levels(region_ageg$ageg)
## NULL
region_ageg$ageg <- factor(region_ageg$ageg,
levels = c("old", "middle", "young"))
class(region_ageg$ageg)
## [1] "factor"
levels(region_ageg$ageg)
## [1] "old" "middle" "young"
ggplot(data = region_ageg, aes(x = region, y = pct, fill = ageg)) +
geom_col() +
coord_flip()+
scale_x_discrete(limits = order)
## Warning: Continuous limits supplied to discrete scale.
## Did you mean `limits = factor(...)` or `scale_*_continuous()`?
## Warning: Removed 21 rows containing missing values (position_stack).
## Warning in rep(yes, length.out = len): 'x' is NULL so the result will be NULL
## Error in ans[ypos] <- rep(yes, length.out = len)[ypos]: replacement has length zero