前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot2画世界地图小实例

ggplot2画世界地图小实例

作者头像
用户7010445
发布2020-03-03 15:02:57
2.9K0
发布2020-03-03 15:02:57
举报

最近可能会用到,先熟悉一下

代码语言:javascript
复制
library(rworldmap)
library(ggplot2)
worldMap <- fortify(map_data("world"), region = "subregion")
table(worldMap$subregion)
table(worldMap$region)
colnames(worldMap)
ggplot()+
  geom_polygon(data=worldMap,aes(x=long,y=lat,group=group))

image.png

填充为白色,边界为绿色

代码语言:javascript
复制
ggplot()+
  geom_polygon(data=worldMap,aes(x=long,y=lat,group=group),
               fill="white",color="darkgreen")+
  theme_bw()

image.png

为不同的国家地区填充不同的颜色

代码语言:javascript
复制
worldMap$region<-ifelse(worldMap$region=="Taiwan","China",worldMap$region)
ggplot()+
  geom_polygon(data=worldMap,
               aes(x=long,y=lat,group=group,fill=region))+
  theme_bw()+
  theme(legend.position = "none")

image.png 指定国家地区填充颜色

代码语言:javascript
复制
worldMap$fill<-ifelse(worldMap$region=="USA" | worldMap$region=="Russia" ,"A","B")
ggplot()+
  geom_polygon(data=worldMap,
               aes(x=long,y=lat,group=group,fill=fill))+
  theme_bw()+scale_fill_viridis_d()+
  theme(legend.position = "none")

image.png 另外一种形式,以下代码来自网络,原文地址 https://stackoverflow.com/questions/54964279/how-to-create-a-world-street-map-with-r

代码语言:javascript
复制
library(sf)
install.packages("tmap")
library(tmap)
library(ggplot2)
data("World")
ggplot(World) +
  geom_sf()+
  theme_minimal()

image.png

以下代码是自己在网上找到的,暂时还看不太懂,原文地址 https://stackoverflow.com/questions/43207947/whole-earth-polygon-for-world-map-in-ggplot2-and-sf

代码语言:javascript
复制
library(dplyr)
library(sf)
library(ggplot2)
library(rnaturalearth)
crs <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +datum=WGS84 +units=m +no_defs"
ctrys50m <- ne_countries(scale = 50, type = "countries", returnclass = "sf") %>%
  select(iso_a3, iso_n3, admin)
sphere <- st_graticule(ndiscr = 10000, margin = 10e-6) %>%
  st_transform(crs = crs) %>%
  st_convex_hull() %>%
  summarise(geometry = st_union(geometry))
ggplot()  +
  geom_sf(data = sphere, fill = "#D8F4FF", alpha = 0.7) +
  geom_sf(data = ctrys50m, fill="grey") +
  theme_bw()+
  theme(panel.border = element_blank())

image.png

为不同的洲添加颜色,以下代码来自网络 原文地址 https://stackoverflow.com/questions/57464602/ggplot2-add-continent-names-to-a-world-map-plot

代码语言:javascript
复制
library(rgdal)
library(broom)
library(ggplot2)
install.packages("svglite")
library(svglite)
library(tidyverse)
library(maptools)
library(raster)
library(rgeos)


### First part is about downloading shapefiles

# load shape files
# download.file("http://naciscdn.org/naturalearth/packages/natural_earth_vector.zip")
world = readOGR(dsn   = "../../Bioinformatics_tools/Earth_Map_data/110m_cultural",
                layer = "ne_110m_admin_0_countries")

world_id    = world@data$CONTINENT
world_union = unionSpatialPolygons(world, world_id)

world_fortified = tidy(world_union, region = "CONTINENT")

results = data.frame(id             = c("Africa", "Asia", "Europe", "North America", "Oceania", "South America"),
                     kpi            = c(20, 30, 50, 50, 60, 70),
                     continent_long = c(15, 80, 20, -100, 150, -60),
                     continent_lat  = c(15, 35, 50, 40, -25, -15),
                     stringsAsFactors = F)

world_for_plot = world_fortified %>%
  left_join(., results, by = "id") %>%
  filter(!is.na(kpi))


plain <- theme(
  axis.text = element_blank(),
  axis.line = element_blank(),
  axis.ticks = element_blank(),
  panel.border = element_blank(),
  panel.grid = element_blank(),
  axis.title = element_blank(),
  panel.background = element_rect(fill = "transparent"),
  plot.background = element_rect(fill = "transparent"),
  plot.title = element_text(hjust = 0.5)
)

raw_plot = ggplot(data = world_for_plot,
                  aes(x = long,
                      y = lat,
                      group = group)) +
  geom_polygon(aes(fill = kpi)) +
  coord_equal(1.3) +
  scale_fill_distiller(palette = "RdYlGn", direction = 1) +
  labs(fill = "kpi") +
  plain
position = coordinates(world_union)

position = data.frame(position, row.names(position))
names(position) = c("long", "lat", "id")

position = position %>%
  filter(id %in% world_for_plot$id)

final_plot = raw_plot +
  geom_text(data = position,
            aes(label = id,
                x = long,
                y = lat,
                group = id))
final_plot = raw_plot +
  geom_text(data = results,
            aes(label = id,
                x = continent_long,
                y = continent_lat,
                group = id))
final_plot

image.png

参考资料
  • https://www.r-spatial.org/r/2018/10/25/ggplot2-sf-2.html
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档