ggplot2 案例:绘制大规模散点图

ggplot2 案例:绘制大规模散点图

本文首先介绍了最近我搜集的一些新的地图数据,包括一个带九段线的世界地图(精度较低),然后通过两个案例(世界人口分布和中国夜间灯光数据)演示了如何使用 ggplot2 绘制大规模散点图。

带九段线的世界地图

1
2
3
4
5
6
7
8
9
10
11
library(sf)
library(ggplot2)

df <- read_sf('world.geo.json')
ggplot(df) +
geom_sf(aes(fill = name), color = "white", size = 0.08) +
scale_fill_viridis_d() +
theme_modern_rc(base_family = enfont) +
worldtilegrid::theme_enhance_wtg() +
theme(legend.position = "none") +
labs(title = "World Map")

我们可以把中国单独拿出来:

1
2
3
4
5
6
7
ggplot(subset(df, name == "China")) + 
geom_sf(aes(fill = name), color = "white", size = 0.08) +
scale_fill_viridis_d() +
theme_modern_rc(base_family = enfont) +
worldtilegrid::theme_enhance_wtg() +
theme(legend.position = "none") +
labs(title = "China Map")

中国市级行政区划图

这份地图数据里面包含了一些空气质量和污染物的数据,这里用 AQI 作为演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cn <- read_sf('JToMOWvicvJOISZFCkEI.json')
# 国界线
boundary <- st_read('quanguo_Line.geojson', stringsAsFactors = FALSE)

cnfont <- 'STLibianSC-Regular'
ggplot(cn) +
geom_sf(aes(fill = aqi), color = "white", size = 0.01) +
geom_sf(data = subset(boundary, QUHUADAIMA == "guojiexian"),
aes(geometry = geometry),
color = "white", size = 0.1) +
scale_fill_viridis_c() +
theme_modern_rc(base_family = cnfont,
subtitle_family = cnfont,
caption_family = cnfont) +
worldtilegrid::theme_enhance_wtg() +
labs(title = "各个市的空气质量指数",
subtitle = "TidyFriday Project",
caption = "数据来源:搜集于网络,仅供展示",
fill = "AQI") +
coord_sf(crs = "+proj=laea +lat_0=23 +lon_0=113 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs")

案例一:世界人口的分布

这里使用的世界地图就是上面介绍的世界地图数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
wd <- read_sf('world.geo.json')
pop <- jsonlite::fromJSON('population.json') %>%
as.data.frame()

ggplot(pop) +
geom_sf(data = wd, fill = "#1E1E1E",
color = "white", size = 0.04) +
geom_point(aes(x = V1, y = V2, color = log(V3)),
size = 0.001) +
scale_color_viridis_c() +
theme_modern_rc(base_family = cnfont,
plot_title_family = cnfont,
subtitle_family = cnfont,
caption_family = cnfont) +
worldtilegrid::theme_enhance_wtg() +
labs(title = "世界人口分布",
subtitle = "TidyFriday Project",
caption = "数据来源:搜集自网络,仅供演示使用",
color = "人口密度对数")

案例二:中国夜间灯光数据(2013年)

全球夜间灯光数据可以从这里下载:https://www.ngdc.noaa.gov/eog/dmsp/downloadV4composites.html 我也尝试了下,就是数据集有点大,完整地绘制整个世界的很困难(最后得到的绘制散点的数据集观测值数量达到了 7 亿)。所以这里我就只绘制中国的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 夜间灯光数据
# https://www.ngdc.noaa.gov/eog/dmsp/downloadV4composites.html
library(readbitmap)
light <- read.bitmap('lightF182013.tiff') %>%
as.data.frame()
dim(light)
colnames(light) <- seq(from = 57.6300459959999998,
to = 135.1066187319999869,
length.out = dim(light)[2])
light$lat <- seq(from = 73.4821902409999979,
to = 3.8150178409999995,
length.out = dim(light)[1])

# 调高向量的长度限制
mem.maxVSize(vsize = cumprod(dim(light))[2])

library(tidyr)
library(dplyr)
light_da <- light %>%
gather(1:1372, key = "long", value = "light")
light_da$long <- as.numeric(light_da$long)

ggplot(subset(light_da, light != 0)) +
geom_point(aes(x = long, y = lat, color = light),
size = 0.001) +
scale_color_gradientn(
colours = c("#1E1E1E", "#1E1E1E", "#fcfff7", "#f1ff8f", "#f1e93f", "#fabd08"),
values = c(0, 0.05, 0.1, 0.25, 0.5, 1.0)) +
theme_modern_rc(base_family = cnfont,
plot_title_family = cnfont,
subtitle_family = cnfont,
caption_family = cnfont) +
worldtilegrid::theme_enhance_wtg() +
theme(legend.position = "none") +
labs(title = "中国夜间灯光数据",
subtitle = "TidyFriday Project",
caption = "数据来源:Earth Observation Group - Defense Meteorological Satellite Progam, Boulder | ngdc.noaa.gov\n<https://www.ngdc.noaa.gov/eog/dmsp/downloadV4composites.html>") +
coord_sf(crs = "+proj=longlat +lat_0=34.3628 +lon_0=115.5268")

我给每个散点设置了经纬度,不过由于原图的坐标系不是 +proj=longlat 型的,所以我这样插值得到的坐标是有问题的。结果就是最后我想在图的下面垫一层中国地图的时候总是对不齐。。。

我再绘制一张东部地区的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 绘制东部地区的(经度范围 100 ~ 137,纬度范围 15 ~ 52)
subdf <- light_da %>%
dplyr::filter(light != 0) %>%
dplyr::filter(lat < 52 & lat > 15) %>%
dplyr::filter(long < 137 & long > 100)
ggplot(subdf) +
geom_point(aes(x = long, y = lat, color = light),
size = 0.001) +
scale_color_gradientn(
colours = c("#1E1E1E", "#1E1E1E", "#fcfff7", "#f1ff8f", "#f1e93f", "#fabd08"),
values = c(0, 0.05, 0.1, 0.25, 0.5, 1.0)) +
theme_modern_rc(base_family = cnfont,
plot_title_family = cnfont,
subtitle_family = cnfont,
caption_family = cnfont) +
worldtilegrid::theme_enhance_wtg() +
theme(legend.position = "none") +
labs(title = "中国东部夜间灯光数据",
subtitle = "TidyFriday Project",
caption = "数据来源:Earth Observation Group - Defense Meteorological Satellite Progam, Boulder | ngdc.noaa.gov\n<https://www.ngdc.noaa.gov/eog/dmsp/downloadV4composites.html>") +
coord_sf(crs = "+proj=laea +lat_0=34.3628 +lon_0=115.5268")

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×