#install.packages(c("cowplot", "googleway","ggplot2", "ggrepel", "ggspatial", "libwgeom", "sf", "rnaturalearth","rnaturalearthdata"))Maps with ggplot2
Setting the world map
library("ggplot2")
theme_set(theme_bw())
library("sf")Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)[1] "sf" "data.frame"
# 必要な変数に絞る
world2<-world[,c(18,45)]ggplot(data = world) +
geom_sf()Data of ownership
library(readxl)
map_ownership <- read_excel("map_ownership.xlsx")
#変数名変更
map_ownership$Ratio <- map_ownership$parent1_share01
map_ownership$iso_a3 <- map_ownership$cty
map_ownership$cty<-NULL
map_ownership$parent1_share01<-NULL
map_ownership$oecd<-NULL
map_ownership$pcrdbofgdp<-NULL
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
world_merged <- left_join(world2, map_ownership, by = "iso_a3")Map of ownership
title1 <- paste0("Average ownership ratio in ",length(map_ownership$Ratio), " host countries (1989-2016)")
ggplot(data = world_merged) +
geom_sf(aes(fill = Ratio)) +
scale_fill_viridis_c(option = "plasma", trans = "sqrt") +
ggtitle(title1)ggsave("fig1_map_ownership_plasma.jpg", width = 8, height = 4, dpi = "print")別のつくり方
https://bookdown.org/mcwimberly/gdswr-book/mapping-with-ggplot2.html
出資比率
cap1 <- paste0(length(map_ownership$Ratio), " host countries (1989-2016)")
ggp1 <-ggplot(data = world_merged) +
geom_sf(aes(fill = Ratio)) +
scale_fill_distiller(name="Ratio", palette = "Reds", direction=1)+
labs(title="",caption=cap1) +
coord_sf(xlim = c(-180, 180), ylim = c(85, -55), expand = TRUE)+
theme(axis.text = element_blank(), axis.title = element_blank())
ggsave("fig1_map_ownership.jpg", width = 8, height = 4, dpi = "print")完全所有の割合
direction=1 or -1 で色の順番を変更できる。
cap1 <- paste0(length(map_ownership$wos), " host countries (1989-2016)")
ggp2 <- ggplot(data = world_merged) +
geom_sf(aes(fill = Ratio)) +
scale_fill_distiller(name="Whole ownership", palette = "Reds", direction=1)+
labs(title="",caption=cap1) +
coord_sf(xlim = c(-180, 180), ylim = c(85, -55), expand = TRUE)+
theme(axis.text = element_blank(), axis.title = element_blank())
ggsave("fig1_map_wos.jpg", width = 8, height = 4, dpi = "print")2つの地図を合わせる
https://statisticsglobe.com/draw-multiple-ggplot-plots-side-by-side
#install.packages("gridExtra") # Install gridExtra package
library("gridExtra") # Load gridExtra package
Attaching package: 'gridExtra'
The following object is masked from 'package:dplyr':
combine
grid.arrange(ggp1, ggp2, nrow = 2) # Apply grid.arrange functionlibrary(rgeos)Loading required package: sp
rgeos version: 0.5-9, (SVN revision 684)
GEOS runtime version: 3.8.0-CAPI-1.13.1
Please note that rgeos will be retired by the end of 2023,
plan transition to sf functions using GEOS at your earliest convenience.
Linking to sp version: 1.5-0
Polygon checking: TRUE
library(rworldmap)### Welcome to rworldmap ###
For a short introduction type : vignette('rworldmap')
library(rworldxtra)
#方法1
# get world map
wmap <- getMap(resolution="high")
# wmapをデータに変換すると重心の緯度経度ある
wmap_data <- as.data.frame(wmap)
library(dplyr)
world_merged$ISO3<-world_merged$iso_a3
world_merged2 <- left_join(world_merged,wmap_data,by="ISO3")
world_merged2$n_aff2<- (world_merged2$n_aff)/500
ggplot() +
geom_sf(data = world_merged2,aes(fill = Ratio)) +
scale_fill_distiller(name="Ratio", palette = "Blues")+
geom_point(data = world_merged2, aes(x = LON, y = LAT), size = world_merged2$n_aff2,
shape = 23, fill = "darkred") Warning: Removed 183 rows containing missing values (geom_point).
References
- Drawing beautiful maps programmatically with R, sf and ggplot2 — Part 1: Basics
- R FOR STATA USERS