之前写了几篇介绍绘制地图的文档,但是过于零散不成体系,今天写了一个更加详细的版本来分享给大家,希望各位观众老爷能够喜欢
绘制地图最简单的方法也许是使用geom_polygon()绘制不同区域的边界。通过ggplot2::map_data()函数获取地图数据,由于它内置在R中因此很容易上手,下面我们来绘制欧洲主要国家的地图来进行演示
加载R包
library(tidyverse)
1. 创建需要绘制的国家名称
some.eu.countries <- c(
"Portugal", "Spain", "France", "Switzerland", "Germany",
"Austria", "Belgium", "UK", "Netherlands",
"Denmark", "Poland", "Italy",
"Croatia", "Slovenia", "Hungary", "Slovakia",
"Czech republic")
2. 从世界地图中提取出上述国家的位置信息
some.eu.maps <- map_data("world", region = some.eu.countries)
3. 创建国家名称标签
根据国家的经纬度信息计算平均值,用来添加名称
long ----> 经度;lat ---->纬度
region.lab.data <- some.eu.maps %>%
group_by(region) %>%
summarise(long = mean(long), lat = mean(lat))
4. 创建需要展示的国家首都经纬度数据集
labs <- data.frame(
long = c(0.1278,2.3522,13.4050,12.5683,2.1734),
lat = c(51.5074,48.8566,52.5200,55.6761,41.3851),
names =c("London","Paris","Berlin","Copenhagen",
"Barcelona"))
由于我们还想把主要的城市用曲线连接起来,因此根据经纬度再创建一个位点信息
countries_lines <- tibble(x = c(0.1278,2.3522,13.4050,12.5683),
xend = c(2.3522,13.4050,12.5683,0.1278),
y = c(51.5074,48.8566,52.5200,55.6761),
yend = c(48.8566,52.5200,55.6761,51.5074))
5. 数据可视化
geom_polygon()函数将绘制每个国家的边界
ggplot(some.eu.maps, aes(x = long, y = lat)) +
geom_polygon(aes( group = group, fill = region)) -> p1
p1
根据set3创建的国家名称标签,对地图进行文本添加
p1 + geom_text(data = region.lab.data,aes(label = region),
size = 4, hjust = 0.5) -> p2
p2
根据经纬度信息添加主要城市信息,此处由于罗马的位置信息与标签重合因此重新叠加了一个几何对象
p2 + geom_point(data = labs,aes(x = long, y = lat,color=names),
size = 3,show.legend = F)+
geom_point(aes(x =12.4964,y =41.9028),
color ="blue",size =3,show.legend = F) -> p3
p3
添加城市文本
p3 + geom_text(data = labs,aes(x = long,y = lat+0.5,
label=names),size=3,hjust=0.5)+
annotate("text",label = "Rome",
x =12.6, y =41.5028,size =3, colour = "black") -> p4
p4
通过线条将主要城市连接起来,此处分别展示了曲线连接与直线连接2中方法。
p4 + geom_curve(data = countries_lines,
aes(x = x,xend=xend,y = y,yend = yend),
size=1,color="green",angle = 90,alpha=1,
arrow = arrow(length = unit(0.01, "npc")))+
geom_segment(aes(x = 2.1734,xend=12.4964,y = 41.3851,
yend = 41.9028),size=1,
color = "blue",inherit.aes=FALSE)+
scale_fill_viridis_d()+
theme_void()+
theme(legend.position = "none")
完整版代码
library(tidyverse)
some.eu.countries <- c(
"Portugal", "Spain", "France", "Switzerland", "Germany",
"Austria", "Belgium", "UK", "Netherlands",
"Denmark", "Poland", "Italy",
"Croatia", "Slovenia", "Hungary", "Slovakia",
"Czech republic")
some.eu.maps <- map_data("world", region = some.eu.countries)
region.lab.data <- some.eu.maps %>%
group_by(region) %>%
summarise(long = mean(long), lat = mean(lat))
labs <- data.frame(
long = c(0.1278,2.3522,13.4050,12.5683,2.1734),
lat = c(51.5074,48.8566,52.5200,55.6761,41.3851),
names =c("London","Paris","Berlin","Copenhagen",
"Barcelona"))
countries_lines <- tibble(x = c(0.1278,2.3522,13.4050,12.5683),
xend = c(2.3522,13.4050,12.5683,0.1278),
y = c(51.5074,48.8566,52.5200,55.6761),
yend = c(48.8566,52.5200,55.6761,51.5074))
ggplot(some.eu.maps, aes(x = long, y = lat)) +
geom_polygon(aes( group = group, fill = region))+
geom_text(data = region.lab.data,aes(label = region),
size = 4, hjust = 0.5) +
geom_point(data = labs,aes(x = long, y = lat,color=names),
size = 3,show.legend = F)+
geom_point(aes(x =12.4964,y =41.9028),
color ="blue",size =3,show.legend = F)+
geom_text(data = labs,aes(x = long,y = lat+0.5,
label=names),size=3,hjust=0.5)+
annotate("text",label = "Rome",
x =12.6, y =41.5028,size =3, colour = "black")+
geom_curve(data = countries_lines,
aes(x = x,xend=xend,y = y,yend = yend),
size=1,color="green",angle = 90,alpha=1,
arrow = arrow(length = unit(0.01, "npc")))+
geom_segment(aes(x = 2.1734,xend=12.4964,y = 41.3851,
yend = 41.9028),size=1,
color = "blue",inherit.aes=FALSE)+
scale_fill_viridis_d()+
theme_void()+
theme(legend.position = "none")
交互式文档
为了方便观看制作了一个交互式文档,喜欢各位能喜欢
网友评论