R パッケージ
一覧library(ggthemes)
library(ggrepel)
library(jpndistrict)
library(magrittr)
library(remotes)
library(rnaturalearth)
library(pacman)
library(RColorBrewer)
library(sf)
library(stargazer)
library(tidyverse)
Choropleth map`
rnaturalearthdata
packagepackman
package enables us to install and load a package without typing install.packges()
at Console on your RStudioinstall.packages("pacman")
We use free data on world map: Natural Earch
Using rnaturalearth
package, we get the data necessary to draw a world map
Type the following command at a chunk on your RStudio and download the data
::p_load(rnaturalearth, rnaturalearthdata, rgeos) pacman
world_map
<- ne_countries(scale = "small", returnclass = "sf") world_map
world_map
names(world_map)
[1] "scalerank" "featurecla" "labelrank" "sovereignt" "sov_a3"
[6] "adm0_dif" "level" "type" "admin" "adm0_a3"
[11] "geou_dif" "geounit" "gu_a3" "su_dif" "subunit"
[16] "su_a3" "brk_diff" "name" "name_long" "brk_a3"
[21] "brk_name" "brk_group" "abbrev" "postal" "formal_en"
[26] "formal_fr" "note_adm0" "note_brk" "name_sort" "name_alt"
[31] "mapcolor7" "mapcolor8" "mapcolor9" "mapcolor13" "pop_est"
[36] "gdp_md_est" "pop_year" "lastcensus" "gdp_year" "economy"
[41] "income_grp" "wikipedia" "fips_10" "iso_a2" "iso_a3"
[46] "iso_n3" "un_a3" "wb_a2" "wb_a3" "woe_id"
[51] "adm0_a3_is" "adm0_a3_us" "adm0_a3_un" "adm0_a3_wb" "continent"
[56] "region_un" "subregion" "region_wb" "name_len" "long_len"
[61] "abbrev_len" "tiny" "homepart" "geometry"
world_map
class(world_map)
[1] "sf" "data.frame"
world_map
has two classes: sf
& data.frame
world_map
library(stargazer)
{r, results = "asis"}
at chunk optionstargazer(as.data.frame(world_map),
type ="html",
digits = 2)
Statistic | N | Mean | St. Dev. | Min | Pctl(25) | Pctl(75) | Max |
scalerank | 177 | 1.01 | 0.15 | 1 | 1 | 1 | 3 |
labelrank | 177 | 3.71 | 1.35 | 2 | 3 | 5 | 7 |
adm0_dif | 177 | 0.08 | 0.28 | 0 | 0 | 0 | 1 |
level | 177 | 2.00 | 0.00 | 2 | 2 | 2 | 2 |
geou_dif | 177 | 0.00 | 0.00 | 0 | 0 | 0 | 0 |
su_dif | 177 | 0.01 | 0.11 | 0 | 0 | 0 | 1 |
brk_diff | 177 | 0.03 | 0.18 | 0 | 0 | 0 | 1 |
mapcolor7 | 177 | 3.23 | 1.72 | 1 | 2 | 5 | 7 |
mapcolor8 | 177 | 3.49 | 1.89 | 1 | 2 | 5 | 8 |
mapcolor9 | 177 | 3.80 | 2.10 | 1 | 2 | 5 | 9 |
mapcolor13 | 176 | 6.33 | 3.56 | 1.00 | 3.00 | 9.00 | 13.00 |
pop_est | 176 | 38,491,454.00 | 137,822,959.00 | 140.00 | 3,481,234.00 | 26,163,376.00 | 1,338,612,970.00 |
gdp_md_est | 176 | 395,512.50 | 1,392,204.00 | 16.00 | 13,197.50 | 235,100.00 | 15,094,000.00 |
pop_year | 4 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
lastcensus | 167 | 2,005.13 | 7.38 | 1,970.00 | 2,002.00 | 2,010.00 | 2,012.00 |
gdp_year | 5 | 401.80 | 898.45 | 0.00 | 0.00 | 0.00 | 2,009.00 |
wikipedia | 1 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | |
name_len | 177 | 8.06 | 3.15 | 4 | 6 | 10 | 22 |
long_len | 177 | 8.79 | 4.53 | 4 | 6 | 10 | 35 |
abbrev_len | 177 | 4.75 | 1.13 | 3 | 4 | 5 | 10 |
tiny | 10 | 3.10 | 1.29 | 2.00 | 2.00 | 4.00 | 5.00 |
homepart | 171 | 1.00 | 0.00 | 1.00 | 1.00 | 1.00 | 1.00 |
pop_est
: population of each countrypop_est
)colors
at Console of your RStudio%>%
world_map ggplot() +
geom_sf(aes(fill = pop_est)) +
scale_fill_gradient(low = "white", high = "green") +
labs(fill = "人口") +
theme_minimal(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom")
%>%
world_map ggplot() +
geom_sf(aes(fill = pop_est)) +
scale_fill_gradient(low = "white", high = "green") +
labs(fill = "人口") +
coord_sf(xlim = c(-10, 45), ylim = c(35, 60)) +
theme_minimal(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom")
<- ne_countries(scale = "medium", returnclass = "sf") %>%
world_map2 mutate(Population = case_when(pop_est < 30000000 ~ "3千万未満",
< 40000000 ~ "4千万未満",
pop_est < 50000000 ~ "5千万未満",
pop_est < 60000000 ~ "6千万未満",
pop_est < 70000000 ~ "7千万未満",
pop_est < 80000000 ~ "8千万未満",
pop_est TRUE ~ "8千万以上"),
Population = factor(Population,
levels = c("3千万未満", "4千万未満", "5千万未満",
"6千万未満", "7千万未満", "8千万未満",
"8千万以上")))
%>%
world_map2 ggplot() +
geom_sf(aes(fill = Population)) +
scale_fill_brewer(palette = "green", drop = FALSE) +
labs(fill = "人口") +
coord_sf(xlim = c(-10, 45), ylim = c(35, 60)) +
geom_label_repel(
data = st_crop(world_map2,
c(xmin = -10, xmax = 45, ymin = 35, ymax = 60)),
aes(label = name, geometry = geometry),
size = 11,
stat = "sf_coordinates",
min.segment.length = 0
+
) theme_minimal(base_family = "HiraKakuProN-W3") +
theme(legend.position = "bottom",
axis.text = element_text(size = 30),
legend.title = element_text(size = 32),
legend.text = element_text(size = 32),
legend.key.size = unit(1, "cm"),
legend.key.width = unit(3,"cm"))
%>%
world_map select(sovereignt, pop_est) %>%
::datatable() DT
jpndistrict
packagejpndistrict
package includes the mapping data needed to draw a map of Japanremotes()
package and download the data from Git
::install_github("uribo/jpndistrict") remotes
jpndistrict
packagelibrary(jpndistrict)
jpndistrict
package and name them df_jpn_map
<- 1:47 %>%
df_jpn_map map(~ jpn_pref(pref_code = ., district = FALSE)) %>%
reduce(rbind) %>%
st_simplify(dTolerance = 0.01)
df_jpn_map
includes:names(df_jpn_map)
[1] "pref_code" "prefecture" "geometry"
%>%
df_jpn_map ggplot() +
geom_sf() +
theme_minimal()
df_jpn_map
) necessary to draw a map of Japanlibrary(tidyverse)
jpn_pop.csv
) and put it into data
folder made in your R Project folder.jpn_pop.csv
and name it df_jpn_pop
<- read_csv("data/jpn_pop.csv",
df_jpn_pop locale = locale(encoding = "cp932"))
df_jpn_pop
head(df_jpn_pop)
# A tibble: 6 x 4
pref prefecture total diff
<chr> <chr> <dbl> <dbl>
1 okinawa 沖縄県 1430000 3
2 tokyo 東京都 13510000 2.7
3 aichi 愛知県 7480000 1
4 kanagawa 神奈川県 9130000 0.9
5 saitama 埼玉県 7260000 0.9
6 fukuoka 福岡県 5100000 0.6
df_jpn_map
and df_jpn_pop
by the shared variable prefecture
and name it df_jap
<- df_jpn_pop %>%
df_jap left_join(df_jpn_map, by = "prefecture") %>%
st_as_sf()
df_jap
::datatable(df_jap) DT
row | variables | details |
---|---|---|
1 | Pref |
Prefecture (English) |
2 | Prefecture |
Prefecture (Japanese) |
3 | total |
Population |
4 | diff |
Rate of increase/decreate between 2010 and 2015(%) |
5 | geometry |
Mapping data |
<- df_jap %>%
map_pop_jpn1 mutate(geometry = if_else(pref == "okinawa", geometry + c(5.5, 17.5), geometry)) %>%
ggplot() +
geom_sf(aes(fill = total), size = .pt / 20) +
annotate("segment",
x = c(128, 132.5, 138),
xend = c(132.5, 138, 138),
y = c(40, 40, 42),
yend = c(40, 42, 46),
size = .pt / 15
+
) scale_fill_distiller(name = "Population",
palette = "Greens", direction = 1) + # 任意の色を指定
theme_map(base_family = "HiraginoSans-W3", base_size = 8) +
theme(legend.position = "right") +
coord_sf(datum = NA)
map_pop_jpn1
fig
fig
with the name you like and the size you likeggsave("fig/map_pop_jpn1.png", map_pop_jpn1, width = 45, height = 45)
・Type the following command at the chunk option: {r, fig.align = 'center', fig.height = 45, fig.width = 45}
<- df_jap %>%
map_pop_jpn2 mutate(geometry = if_else(pref == "okinawa", geometry + c(5.5, 17.5), geometry)) %>%
ggplot() +
geom_sf(aes(fill = diff), size = .pt / 20) +
annotate("segment",
x = c(128, 132.5, 138),
xend = c(132.5, 138, 138),
y = c(40, 40, 42),
yend = c(40, 42, 46),
size = .pt / 15
+
) scale_fill_distiller(name = "Population Increase/Decrease Rates(%)",
palette = "RdBu", direction = 1) +
theme_map(base_family = "HiraginoSans-W3", base_size = 8) +
theme(legend.position = "right") +
coord_sf(datum = NA)
map_pop_jpn2
fig
fig
with the name you like and the size you likeggsave("fig/map_pop_jpn2.png", map_pop_jpn2, width = 45, height = 45)
Tip
The Map of Japan with Japan’s Lower House election data is useful. This map was made by Mr.Ikuya Nakayama: 「2014年衆議院総選挙選挙区比較マップ」
Q1: Draw a map of Japan with the data you like
参考文献F@N Communications, Inc.
) からご助言いただいた