animation包——立体世界地图绘制

animation包——立体世界地图绘制

本文是微信推文的学习笔记。记不起是哪篇微信推文了。

区分七个大洲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
library(ggplot2)
library(maps)
library(plyr)
library(grid)
library(showtext)
library(Cairo)
library(xlsx)
library(RColorBrewer)
library(dplyr)
library(animation)
setwd("~/Desktop")
world_map <- map_data("world")
data <- read.csv("World_State.csv", stringsAsFactors = F, check.names = F) #一个标明每个国家在那个洲的表格
ggplot(data, aes(map_id = region)) +
geom_map(aes(fill = Address), map = world_map, col = "white") +
expand_limits(x = world_map$long, y = world_map$lat) +
scale_y_continuous(breaks = (-2:2)*30) +
scale_x_continuous(breaks = (-4:4)*45) +
coord_map("ortho", orientation = c(30, 110, 0))

筛选某个洲进行特定角度的呈现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
##筛选某个洲进行特定角度的呈现:
data1 <- data[data$Address == "North America",]
ggplot() +
geom_map(data = data, aes(map_id = region), map = world_map, col = "white", fill = "#A3A3A3") +
geom_map(data = data1, aes(map_id = region, fill = Address), map = world_map, col = NA) +
expand_limits(x = world_map$long, y = world_map$lat) +
scale_y_continuous(breaks = (-6:6)*15) +
scale_x_continuous(breaks = c(-12:12)*15) +
coord_map("ortho", orientation = c(0,-95,0)) +
guides(fill = FALSE) +
theme(
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
panel.background = element_rect(fill = "white", colour = NA),
panel.grid.major = element_line(colour = "grey60", size = 0.25),
panel.grid.minor = element_line(colour = "grey60", size = 0.25)
)

计算每个大洲的中心(个别中心离大陆太远需要手动调整)

1
2
3
4
5
6
7
8
9
##计算每个大洲的中心(个别中心离大陆太远需要手动调整)
world_map_data <- merge(world_map, data, all.x = T)
midpos <- function(x) mean(range(x, na.rm = T))
centres <- ddply(world_map_data, .(Address), colwise(midpos, .(long, lat)))
centres <- centres[centres$Address != "Antarctica",]
centres$angle <- 0
centres$long[centres$Address == "Asia"] = 100
centres$long[centres$Address == "North America"] = -100
centres$long[centres$Address == "Oceania"] = 130

使用grid的版本控制系统进行多图排版

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
##使用grid的版本控制系统进行多图排版
world_map_data <- arrange(world_map_data, group, order)
Cairo(file = "worldmap.png", width = 1600, height = 1200)
showtext_begin()
grid.newpage()
pushViewport(viewport(layout = grid.layout(2,3)))
vlayout <- function(x,y) {
viewport(layout.pos.row = x, layout.pos.col = y)
}
for(i in 1:nrow(centres)){
mydata <- world_map_data[world_map_data$Address == centres$Address[i],]
p <- ggplot() +
geom_polygon(data = world_map_data, aes(x = long, y = lat, group = group), fill = "grey95", colour = "grey", size = 0.25) +
geom_map(data = mydata, aes(map_id = region), map = world_map_data, colour = "white", fill = "#F8766D", size = 0.25) +
coord_map("ortho", orientation = c(centres$lat[i], centres$long[i], 0)) +
scale_y_continuous(breaks = (-6:6)*15) +
scale_x_continuous(breaks = (-12:12)*15) +
labs(title = centres$Address[i]) +
theme(
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
text = element_text(size = 20),
panel.background = element_rect(fill = "white", colour = NA),
panel.grid.major = element_line(colour = "grey60", size = 0.25),
panel.grid.minor = element_line(colour = "grey60", size = 0.25),
plot.title = element_text(size = 20, family = "myfont", hjust = 0.5),
plot.margin = unit(c(ifelse(i <= 3, 5, 0.5), 1, ifelse(i >= 3, 5, 0.5), 1), "lines")
)
print(p, vp = vlayout(ifelse(i <= 3, 1, 2), ifelse(i <= 3, i, i-3)))
}

grid.text(label = "God's Perspective", x = 0.01, y = 0.98, gp = gpar(col = "black", fontsize = 35), draw = T, just = "left")
showtext_end()
dev.off()

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了607.9k个字了!

Your browser is out-of-date!

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

×