Don’t forget to load a library:

library("lingtypology")

1. File management

1.1 Saving from R

There are two ways: base R and readr package. I mostly use the second way, since the base R functions haven’t changed for decades, but for purpuses of simplicity will show you everything with base R functions.

ejective_and_n_consonants

First we need to know getwd() and setwd() functions for setting a working directory.

getwd()
setwd("/...") # use backslashes `\`, not slashes `/`!

So now we can save the dataframe:

write.csv(ejective_and_n_consonants, "my_file_name.csv", row.names = FALSE)

1.2 Loading data to R

We can read to R a dataset about Numeral Classifiers from AUTOTYP database.

new_df <- read.csv("https://raw.githubusercontent.com/autotyp/autotyp-data/master/data/Numeral_classifiers.csv")
new_df

It could be also a file on your computer, just provide a whole path to the file. Windows users need to change backslashes \ to slashes /.

new_df_2 <- read.csv("/home/agricolamz/my_file.csv")

1.3 Missing data

In R, missing values are represented by the symbol NA (not available), so ufter uploading all blanks that you have in your table will be changed to NA.

is.na(c(5, 4, NA))
[1] FALSE FALSE  TRUE
sum(is.na(c(5, 4, NA)))
[1] 1

2. Set pop-up boxes

Sometimes it is a good idea to add some additional information (e.g. language affiliation, references or even examples) to pop-up boxes that appear when points are clicked. So I create an extra vector of strings in the dataframe:

df <- data.frame(language = c("Adyghe", "Kabardian", "Polish", "Russian", "Bulgarian"),
                 features = c("polysynthetic", "polysynthetic", "fusional", "fusional", "fusional"))
df$popup <- aff.lang(df$language)

The function aff.lang() creates a vector of genealogical affiliations that can be easily mapped:

map.feature(languages = df$language, 
            features = df$features, 
            popup = df$popup)

Pop-up strings can contain HTML tags, so it is easy to insert a link, a couple of lines, a table or even a video and sound. Here is how pop-up boxes can demonstrate language examples:

# change a df$popup vector
df$popup <- c("sɐ s-ɐ-k'ʷɐ <br> 1sg 1sg.abs-dyn-go <br> 'I go'",
              "sɐ s-o-k'ʷɐ <br> 1sg 1sg.abs-dyn-go <br> 'I go'",
              "id-ę <br> go-1sg.npst <br> 'I go'",
              "ya id-u <br> 1sg go-1sg.npst <br> 'I go'",
              "id-a <br> go-1sg.prs <br> 'I go'")

# create a map
map.feature(languages = df$language,
            features = df$features,
            popup = df$popup)

How to say moon in Sign Languages? Here is an example:

# Create a dataframe with links to video
sign_df <- data.frame(languages = c("American Sign Language", "Russian Sign Language", "French Sign Language"),
                      popup = c("https://media.spreadthesign.com/video/mp4/13/48600.mp4", "https://media.spreadthesign.com/video/mp4/12/17639.mp4", "https://media.spreadthesign.com/video/mp4/10/17638.mp4"))

# Change popup to an HTML code
sign_df$popup <- paste("<video width='200' height='150' controls> <source src='",
                       as.character(sign_df$popup),
                       "' type='video/mp4'></video>", sep = "")
# create a map
map.feature(languages = sign_df$languages, popup = sign_df$popup)

3. Set shapes

For some papers it is not possible to use colors for destinguishing features. In that cases it is posible to use shape argument:

map.feature(languages = ejective_and_n_consonants$language,
            features = ejective_and_n_consonants$ejectives,
            shape = TRUE)

It is possible to provide a vector with corresponding shapes:

map.feature(languages = ejective_and_n_consonants$language,
            features = ejective_and_n_consonants$ejectives,
            label = ejective_and_n_consonants$language,
            shape = c("–", "+"))

There is also possible to use numbers for pointing sumthing:

map.feature(languages = ejective_and_n_consonants$language,
            features = ejective_and_n_consonants$language,
            shape = 1:19) # 1:19 is a short way to say c(1, 2, 3, ..., 19)

4. Set coordinates

You can set your own coordinates using the arguments latitude and longitude. It is important to note, that lingtypology works only with decimal degrees (something like this: 0.1), not with degrees, minutes and seconds (something like this: 0° 06′ 0″). I will illustrate this with the dataset circassian built into the lingtypology package. This dataset comes from fieldwork collected during several expeditions in the period 2011-2016 and contains a list of Circassian villages:

head(circassian)

In this dataframe you can find variables latitude and longitude that could be used:

map.feature(languages = circassian$language,
            features = circassian$dialect,
            label = circassian$village,
            latitude = circassian$latitude,
            longitude = circassian$longitude,
            title = "Circassian dialects")

5. Set layouts

It is possible to use different tiles on the same map using the tile argument. For more tiles look here.

map.feature(languages = circassian$language,
            features = circassian$dialect,
            label = circassian$village,
            latitude = circassian$latitude,
            longitude = circassian$longitude,
            tile = "OpenTopoMap")

It is possible to use different map tiles on the same map. Just add a vector with tiles.

map.feature(languages = circassian$language,
            features = circassian$dialect,
            label = circassian$village,
            latitude = circassian$latitude,
            longitude = circassian$longitude,
            tile = c("OpenStreetMap.BlackAndWhite", "OpenTopoMap"))

6. Minicharts

The argument minichart allows you to add piecharts or barplots instead of standard point markers:

map.feature(languages = ejective_and_n_consonants$language,
            minichart.data = ejective_and_n_consonants[, c("vowels", "consonants")],
            minichart = "bar")
map.feature(languages = ejective_and_n_consonants$language,
            minichart.data = ejective_and_n_consonants[, c("vowels", "consonants")],
            minichart = "pie")
map.feature(languages = ejective_and_n_consonants$language,
            minichart.data = ejective_and_n_consonants[, c("vowels", "consonants")],
            minichart = "pie",
            minichart.labels = TRUE)

7. Add a density contourplot to a map

Sometimes it is easier to look at a density contourplot. It can be created using density.estimation argument. There are two possibility for creation a density contourplot in lingtypology:

  • density.method = "fixed distance". First algorithm creates circle polygons with fixed radius around each point and then merge all polygons that are overlapped. It has only one parameter that should be estimated: radius of the circle (density.width).
  • density.method = "kernal density estimation". Second algorithm uses a kernal density estimation and has two parameters that should be estimated: latitude and longitude bandwidths (density.width[1] and (density.width[2]))
map.feature(circassian$language,
            longitude = circassian$longitude,
            latitude = circassian$latitude,
            density.estimation = circassian$language,
            density.width = 0.15)

It is possible to remove points and display only the kernal density estimation plot, using the density.points argument:

map.feature(circassian$language,
            longitude = circassian$longitude,
            latitude = circassian$latitude,
            density.estimation = circassian$language,
            density.width = 0.15,
            density.points = FALSE)

If you want to use kernal density estimation, you need to change method type and provide a vector of parameters that increase/decrease area:

map.feature(circassian$language,
            features = circassian$language,
            longitude = circassian$longitude,
            latitude = circassian$latitude,
            density.estimation = "Circassian",
            density.method = "kernal density estimation",
            density.width = c(0.3, 0.3))
map.feature(circassian$language,
            features = circassian$language,
            longitude = circassian$longitude,
            latitude = circassian$latitude,
            density.estimation = "Circassian",
            density.method = "kernal density estimation",
            density.width = c(0.7, 0.7))
map.feature(circassian$language,
            features = circassian$language,
            longitude = circassian$longitude,
            latitude = circassian$latitude,
            density.estimation = "Circassian",
            density.method = "kernal density estimation",
            density.width = c(1.3, 0.9))

It is important to note, that this type of visualization have some shortcomings. The kernel density estimation is calculated without any adjustment, so longitude and latitude values used as a values in Cartesian coordinate system. To reduce consequences of that solution it is better to use a different coordinate projection. That allows not to treat Earth as a flat object.

8. Graticule

map.feature("Adyghe",
            zoom.level = 12,
            graticule = 0.05) 

9. Minimap

map.feature(c("Russian", "Adyghe"),
            minimap =  TRUE)

10. mapview package

The mapview package provides a possibility to create a multiple maps in a grid and even synchronise them. There are two functions for that: latticeview() and sync(). Facetisation is a really powerfull tool (look for facet_grid() and facet_wrap() functions from ggplot2). lingtypology doesn’t provide a facetisation itself, but the facet argument of the map.feature() function create a list of maps based on this variable. The result of the work of this function then is changed: instead of creating a map in Viewer pane it will return a list that could be used in latticeview() and sync() functions from the mapview package.

faceted <- map.feature(circassian$language,
                       latitude = circassian$latitude,
                       longitude = circassian$longitude,
                       features = circassian$dialect,
                       facet = circassian$language)
library(mapview)
sync(faceted, no.initial.sync = FALSE)

As you can see we provided a circassian$language to the facet argument, so it returned a list of two maps that stored in faceted variable.

It is also possible to combine any maps that were created, just store them in a variable, and combine them in latticeview() and sync() functions

m1 <- map.feature(lang.aff("Tsezic"), label = lang.aff("Tsezic"))
m2 <- map.feature(lang.aff("Avar-Andi"), label = lang.aff("Avar-Andi"))
sync(m1, m2)

11. Create your own atlas with rmarkdown

This section is inspired by talk with Niko Partanen. It is possible to create an atlas website using lingtypology and rmarkdown packages. The function atlas.database() creates a folder in the working directory that contains an rmarkdown template for a web-site.

First, lets create a dataframe with some data.

df <- wals.feature(c("1a", "20a"))

Second we can create a website using atlas.database() function:

  • languages argument is a language list
  • features argument is a data.frame with corresponding features
  • latitude and longitude arguments are optional
atlas.database(languages = df$language,
               features = df[,c(4:5)],
               latitude = df$latitude,
               longitude = df$longitude,
               atlas_name = "Some WALS features",
               author = "Author Name")

We can see that this function creates a subfolder with following files:

list.files("./atlas_Some_WALS_features/")
[1] "1._1a.Rmd"    "2._20a.Rmd"   "database.csv" "footer.html" 
[5] "index.Rmd"    "_site.yml"   

The last step is to run a command:

rmarkdown::render_site("./atlas_Some_WALS_features/")

Then the atlas website will be created (here is a result). If you want to change something in the website, just change some files:

  • write information about atlas in index.Rmd file
  • list citation information
  • change any .Rmd file
  • and on the end rerun the rmarkdown::render_site("./atlas_Some_WALS_features/") command.


© G. Moroz, 09.2018. Source code on GitHub