others

ggplot2画世界地图小实例

2019-12-12  本文已影响0人  小明的数据分析笔记本

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

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

填充为白色,边界为绿色

ggplot()+
  geom_polygon(data=worldMap,aes(x=long,y=lat,group=group),
               fill="white",color="darkgreen")+
  theme_bw()
image.png

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

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

指定国家地区填充颜色

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

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

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
参考资料

还有好多问题不理解,需要仔细看代码呀!

欢迎大家关注我的公众号
小明的数据分析笔记本

公众号二维码.jpg
上一篇下一篇

猜你喜欢

热点阅读