0
点赞
收藏
分享

微信扫一扫

ggplot2绘制欧洲地图

墨春 2021-09-19 阅读 91

加载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. 创建国家名称标签

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. 数据可视化

ggplot(some.eu.maps, aes(x = long, y = lat)) +
  geom_polygon(aes( group = group, fill = region)) -> p1

p1

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

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")

交互式文档

举报

相关推荐

0 条评论