今天小编就根据第七次全国人口普查数据进行一些可视化图表的绘制,涉及的知识点较为简单,主要就是一些细节上的定制化操作(推文中使用的数据免费获取方式见文末),主要内容包括:
这一部分小编就使用人口排名前14的数据绘制我们常见的柱形图,可视化代码如下(重要的部分会单独说明):
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(wesanderson)
library(LaCroixColoR)
library(RColorBrewer)
# 读取Excel数据包
library(readxl)
df2<-read_excel("七次.xlsx")
df2$pop <- as.numeric(df2$pop)
data_label = df2 %>% mutate(city = fct_reorder(city, pop),
label=glue::glue("{format(round(pop/1000000, 2), nsmall = 2)} M"))
#可视化绘制
ggplot(data = df2 %>% mutate(city = fct_reorder(city, pop)) %>% top_n(14),
aes(x=city,y=pop)) +
geom_col(aes(fill = city), width = 0.65) +
geom_hline(yintercept = 0, color = "grey50", size = 0.5) +
geom_hline(data = tibble(y = seq(from=0, to=125000000, by=25000000)), aes(yintercept = y),
color = "grey50", size = 0.2, linetype = "dotted") +
geom_text(data = data_label%>%mutate(city = fct_reorder(city, pop)) %>% top_n(14),
aes(x=city,y=pop+8000000,label=label),color = "grey90",
size = 3, family = "Roboto_Mono",
nudge_y = 0.08, hjust = .5)+
scale_fill_manual(values = lacroix_palette(type = "paired"))+
#scale_fill_manual(lacroix_palette("PeachPear", n = 32, type = "continuous")) +
coord_flip(clip = "off") +
labs(
title = "第七次全国人口普查数据结果可视化再现",
subtitle="数据来源:<span style='color:#E53A40'>国家统计局</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")+
scale_y_continuous(limits = c(0, 140000000), breaks = seq(from=0, to=125000000, by=25000000),
labels = scales::comma_format(),
expand = c(0.01, 0.01), position = "right") +
theme( plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "white",family = "kaiti",
face = 'bold',
size = 26, margin = margin(t = 25, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = 0.5,size=12,family = "kaiti",
color = "gray",margin = margin(t = 10, b = 10)),
plot.caption = element_markdown(face = 'bold',size = 8,family = "Roboto_Mono",color = "gray",
margin = margin(t = 20, b = 10)),
legend.position = "none",
plot.background = element_rect(fill = "#323332",
color = "#323332"),
panel.background = element_rect(fill = NA, color = NA),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_text(size=12,family = "kaiti",color = "white",hjust = .5),
axis.title = element_blank(),
plot.margin = margin(10, 50, 10,50),
panel.spacing.y = unit(0, "pt"),
panel.spacing.x = unit(0, "pt"))
结果如下:
全国人口排名前14的可视化展现结果
小编提醒:
scale_x_continuous(labels=scales::unit_format(suffix="k",
scale=0.001,
sep=""))
#或者
scale_x_continuous(labels = scales::comma_format())
由于第七次全国人口普查数据中,占比类型的数据较多,小编这里就使用年龄段人数占比进行可视化图表绘制,可视化代码如下:
data <- data.frame(
class=c("0-14岁", "15-59岁", "60岁及以上"),
prop=c(17.95, 63.35, 18.7)
)
data$ymax <- cumsum(data$prop)
data$ymin <- c(0, head(data$ymax, n=-1))
data$labelPosition <- (data$ymax + data$ymin) / 2
data$label <- paste0(data$class, "人数占比")
# 可视化绘制如下
ggplot(data,aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=as.factor(class))) +
geom_rect(color="#323332",size=1.5) +
geom_label(data = data %>% filter(!prop==max(prop)),
x=4,aes(y=labelPosition, label=paste0(prop, "%","\n",label)),size=4,
alpha = .6,
family = "kaiti",
fontface = "bold")+
geom_text(data = data %>% filter(prop==max(prop)),
aes(x = 1.2, y = 0,label=paste0(prop, "%")),
color="#1f8eff",
family = "Open_Sans",
fontface = "bold",
size = 16) +
geom_text(data = data %>% filter(prop==max(prop)),
aes(x = 0, y = 0,label = label),
color = "#1f8eff",
alpha = .6,
family = "kaiti",
fontface = "bold",
size = 8) +
scale_fill_manual(values = cols2) +
scale_color_manual(values = cols2) +
labs(
title = "第七次全国人口普查数据结果可视化再现",
subtitle="数据来源:<span style='color:#E53A40'>国家统计局</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>")+
coord_polar(theta="y") +
xlim(c(0, 4)) +
#theme_void() +
theme( plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "gray",family = "kaiti",
face = 'bold',
size = 25, margin = margin(t = 25, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = 0.5,size=12,family = "kaiti",color = "gray"),
plot.caption = element_markdown(face = 'bold',size = 10,family = "Roboto_Mono",color = "gray",
margin = margin(t = 1, b = 22)),
legend.position = "none",
plot.background = element_rect(fill = "#323332",
color = "#323332"),
panel.background = element_rect(fill = NA, color = NA),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
plot.margin = margin(10, 120, 10,120),
panel.spacing.y = unit(0, "pt"),
panel.spacing.x = unit(0, "pt"))
全国人口年龄占比
小编提醒:
data %>% filter(prop==max(prop))
data %>% filter(!prop==max(prop))
windowsFonts(
kaiti = windowsFont("STKAITI"),
Cinzel = windowsFont("Cinzel"),#这里使用的是字体的主题名称
Poppins = windowsFont("Poppins"),
IBMPSBold = windowsFont("IBMPlexSans-Bold"),
Roboto_Mono = windowsFont("Roboto Mono"),
Open_Sans = windowsFont("Open Sans"),
Open_Sans_ExtraBold = windowsFont("Open Sans ExtraBold"),
Times_New_Roman = windowsFont("Times New Roman")
)
这一部分当然必不可少了,之前的推文也介绍了很多次,我们直接给出代码即可:
library(rcartocolor)
china_shp <- "中国省级地图GS(2019)1719号.geojson"
nine <- "九段线GS(2019)1719号.geojson"
china <- sf::read_sf(china_shp)
nine_line <- sf::read_sf(nine)
df3<-read_excel("map_china.xlsx")
df3$pop <- as.numeric(df3$pop)
join_data <- left_join(china,df3,by = c("CNAME"="city")) %>% st_as_sf()
join_data_pro <- st_transform(join_data, 2343)
nine_line_pro <- st_transform(nine_line, 2343)
#可视化绘制
ggplot()+
geom_sf(data = join_data_pro, aes(geometry = geometry, fill = pop)) +
geom_sf(data = nine_line_pro,size=1,colour="gray40") +
rcartocolor::scale_fill_carto_c(palette = "BluYl", name = "POP",
direction = -1,
limits = c(0,140000000),
breaks = seq(from=0, to=125000000, by=25000000),
labels = glue::glue("{seq(0, 125, by = 25)}M"),
guide = F) +
guides(fill = guide_colorbar(barheight = unit(2.3, units = "mm"),
barwidth = unit(75, units = "mm"),
direction = "horizontal",
ticks.colour = "grey40",
title.position = "left",
label.position = "top",
title.vjust = 0)) +
labs(
title = "第七次全国人口普查数据结果可视化再现",
subtitle="数据来源:<span style='color:#E53A40'>国家统计局</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
theme( plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "white",family = "kaiti",
face = 'bold',
size = 26, margin = margin(t = 25, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = 0.5,size=12,family = "kaiti",
color = "gray",margin = margin(t = 10, b = 10)),
plot.caption = element_markdown(face = 'bold',size = 8,family = "Roboto_Mono",color = "gray",
margin = margin(t = 20, b = 10)),
legend.position = "top",
legend.title = element_text( color = "white", face = "bold", size = 9,family = "Roboto_Mono"),
legend.text = element_text(color = "white", size = 8,family = "Roboto_Mono"),
legend.background = element_rect(fill = "#323332"),
plot.background = element_rect(fill = "#323332",
color = "#323332"),
panel.background = element_rect(fill = NA, color = NA),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_text(size=12,family = "kaiti",color = "white",hjust = .5),
axis.title = element_blank(),
plot.margin = margin(10, 160, 10,160),
panel.spacing.y = unit(0, "pt"),
panel.spacing.x = unit(0, "pt"))
全国人数据的地图可视化展示
这期推文教程,小编使用了第七次全国人口普查数据进行了部分数据的可视化图表绘制,希望给小伙伴们不一样的视觉体验。同时也希望大家可以掌握更多的图表定制化操作小技巧。
整理不易,感谢大家帮忙分享,关注本公众号(DataCharm)然后在公众号后台发送
练习数据07
即可获知免费下载链接。