Skip to content

Commit

Permalink
new vignette, not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
andysouth committed Sep 8, 2018
1 parent a8fed80 commit 5fc361f
Showing 1 changed file with 154 additions and 0 deletions.
154 changes: 154 additions & 0 deletions vignettes/language-support.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
title: "Language support in rnaturalearth"
author: "Andy South"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
#to produce a pdf
#output: rmarkdown::pdf_document
vignette: >
%\VignetteIndexEntry{What is a country ?}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---

(using languages but also about using with tmap and ggplot)

This vignette shows how [rnaturalearth](https://github.com/ropensci/rnaturalearth) makes it easier to make maps with labels in languages other than Engish, and to make thematic maps if you have data that is referenced in languages other than Engish.

[rnaturalearth](https://github.com/ropensci/rnaturalearth) is an R package to hold and facilitate interaction with natural earth vector map data.

[Natural Earth](http://www.naturalearthdata.com/) is a public domain map dataset including vector country boundaries.


#### load required packages
```{r, eval=TRUE, echo=TRUE, message=FALSE}
library(rnaturalearth)
library(sp)
library(sf)
library(ggplot2)
library(ggrepel)
library(tmap)
```


### Country maps with labels in other languages

```{r, eval=TRUE, echo=TRUE, message=FALSE}
# Africa
sp::plot(ne_countries(continent = 'africa'))
sfaf <- ne_countries(continent = 'africa', returnclass = 'sf')
sfafc <- st_centroid(sfaf)
sfaf <- cbind(sfaf, st_coordinates(st_centroid(sfaf$geometry)))
#G = cbind(G, st_coordinates(st_centroid(G$geometry)))
#this adds centroids in the middle of countries
ggplot(sfaf) +
geom_sf() +
geom_sf(data=sfafc)
#trying labels in the middle of countries, doesn't quite work needs x,y,label
#but once x & y added on with st_coordinates ...
#seems getting the coords might not be necessary for much longer https://github.com/slowkow/ggrepel/issues/111
#cool nearly there ...
ggplot(sfaf) +
geom_sf() +
geom_text_repel(aes(x=X, y=Y, label=name))
#geom_text_repel(data=sfafc, aes(x=X, y=Y, label=name))
#geom_label_repel(data=sfafc, aes(x=X, y=Y, label=name))
#geom_text(data=sfafc, label='name_es', x='X', y='Y')
#getting there, labels still overlap a bit
#maybe make map bigger to allow space for labels
ggplot(sfaf) +
geom_sf() +
geom_text_repel(aes(x=X, y=Y, label=name_es))
# point.padding=NA allows labels to overlap the centroid
ggplot(sfaf) +
geom_sf() +
geom_text_repel(aes(x=X, y=Y, label=name_es), point.padding = NA)
#Africa labels just down left & right sides
#works pretty well I think
ggplot(sfaf) +
geom_sf() +
xlim(-28,61) +
geom_text_repel(aes(x=X, y=Y, label=name_es),
data = subset(sfaf, X > 21),
nudge_x = 60 - subset(sfaf, X > 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 0
) +
geom_text_repel(aes(x=X, y=Y, label=name_es),
data = subset(sfaf, X < 21),
nudge_x = -19 - subset(sfaf, X < 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 1
)
#french labels
ggplot(sfaf) +
geom_sf() +
xlim(-28,61) +
geom_text_repel(aes(x=X, y=Y, label=name_fr),
data = subset(sfaf, X > 21),
nudge_x = 60 - subset(sfaf, X > 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 0
) +
geom_text_repel(aes(x=X, y=Y, label=name_es),
data = subset(sfaf, X < 21),
nudge_x = -19 - subset(sfaf, X < 21)$X,
segment.size = 0.2,
segment.color = "grey50",
direction = "y",
hjust = 1
)
#tmap good but labels currently overlap
#english labels
tm_shape(sfaf) +
tm_borders() +
tm_text("name")
#spanish labels
tm_shape(sfaf) +
tm_borders() +
tm_text("name_es")
#other languages de, fr, nl,
tm_shape(sfaf) +
tm_borders() +
tm_text("name_de", size=0.5)
#chinese labels don't currently work, because attribute data is filled with NA
#I suspect I may need to do something different on reading the data in ?
tm_shape(sfaf) +
tm_borders() +
tm_text("name_zh")
# The full list of
# languages is: name_ar, name_bn, name_de, name_en, name_es, name_fr, name_el,
# name_hi, name_hu, name_id, name_it, name_ja, name_ko, name_nl, name_pl,
# name_pt, name_ru, name_sv, name_tr, name_vi, and name_zh.
# A 2-character language code decoder ring is here:https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes.
```



0 comments on commit 5fc361f

Please sign in to comment.