pyramid_chart解决中文乱码
library(tidyverse)
library(lubridate)
library(showtext)
library(janitor)
library(openxlsx)
library(magrittr)
library(hablar)
library(readxl)
library(ggcharts)
showtext_auto(enable = TRUE)
df <- readxl::read_excel('/mnt/c/users/xuefe/Downloads/grda_fnl_tj.xlsx',skip = 1) %>%
pivot_longer(cols=`0岁`:`100岁`,names_to = 'age',values_to = 'count') %>%
filter(单位=='合计' & age!='0岁') %>%
mutate(age=str_remove_all(age,'岁')) %>%
convert(num(age))
df_m <- df %>%
mutate(age_group=cut(age, breaks =seq(0,110,by=3),include.lowest = F,right=T)) %>%
group_by(age_group) %>%
summarise(sumn=sum(count,na.rm = T)) %>%
mutate(sex='男性')
# right=T 左开右闭
# rihht=F 左闭右开
p_1 <- ggplot(data = df_m) +
geom_bar(aes(age_group,sumn), fill = 'skyblue', stat="identity", position="dodge")+
xlab('年龄组') +
ylab('男性人口数')+
coord_flip()
df <- readxl::read_excel('/mnt/c/users/xuefe/Downloads/grda_fnl_tj (1).xlsx',skip = 1) %>%
pivot_longer(cols=`0岁`:`100岁`,names_to = 'age',values_to = 'count') %>%
filter(单位=='合计' & age!='0岁') %>%
mutate(age=str_remove_all(age,'岁')) %>%
convert(num(age))
df_f <- df %>%
mutate(age_group=cut(age, breaks =seq(0,110,by=3),include.lowest = F,right=T)) %>%
group_by(age_group) %>%
summarise(sumn=sum(count,na.rm = T)) %>%
mutate(sex='女性')
popu <- bind_rows(df_m,df_f)
popu$sumn <- popu$sumn/10000
pyramid_chart(popu,age_group,sumn,group = sex)
#解决中文乱码
font_files()
showtext_auto(enable = TRUE)
# showtext_auto()函数,放在绘图命令前可以自动调用出我们设置的字体
font_add('Songti', 'Songti.ttc')
quartz(family='Songti')
评论
发表评论