I made a tutorial on how to plot typological data (here from grambank) with R.
Here is the code.
library(ggplot2) # for creating nice plots
library(dplyr) # for manipulating data frames
library(sf) # for manipulation of shape files
library(rnaturalearth) # for geographic data
library(rnaturalearthhires) # for high resolution geographic data
library(lingtypology) # for access to databases such as glottolog
library(viridis) # for colorblind-friendly color palettes
# Download to a temporary file
grambank057 <- tempfile(fileext = ".geojson")
download.file(
"https://grambank.clld.org/parameters/GB057.geojson",
grambank057
)
# Read the downloaded geoJson file with the sf library:
grambank <- read_sf(grambank057)
head(grambank)
# create base map of Cameroon
cameroon <- ne_states(country = "Cameroon", returnclass = "sf")
# filter out all data points from grambank that are not in Cameroon
camerbank <- st_filter(grambank, cameroon)
head(camerbank)
#Grambank
ggplot() +
geom_sf(data=cameroon)+
theme_void() +
geom_sf(data = camerbank, aes(fill=label), pch=21, size=2)+
scale_fill_viridis(option="inferno", discrete=TRUE)
I used to plot my typological language data to geo-spatial maps with Generic Mapping Tools, which is awesome, and where a simple two-liner will do the job. But I found this hard to use in teaching, since it doesn’t run smoothly on everyone’s operating system. So it’s time for me to move on and learn to do maps with R. There are a few awesome resources out there, including lingtypology, which reads data directly off glottolog.
But I wanted to plot data that is not included in a database, and since it’s actually easy, but not entirely trivial to find on the internet so far, here’s a short tutorial. First of all, here is our little data set, with geo-spatial coordinates for each language, language family and basic word order info. Save this to a text file in your working directory with the name “typology.txt”.
Read in your map data and your language coordinates and save them to short variables like “map” and “df” (for data frame).
df <- read.csv("typology.txt")
head(df) #shows you the beginning of the data, good for trouble shooting
map <- map_data("world")
In order to plot your language coordinates with additional information about language families, do this:
#family
ggplot() +
geom_polygon(data = map, aes(x = long, y = lat, group=group), fill="#dddddd") + #plots the world map in the background in light grey
geom_point(data = df, aes(x = Longitude, y = Latitude, fill = factor(Family)), shape=21, size=3) + #plots the language coordinates
theme_minimal()+ #fewer embellishments
coord_sf()+ #nice proportions
scale_fill_viridis_d(option = "plasma")+ #color scheme
labs(fill = "Family", y="",x="") #labels
To get information about word order patterns, instead use this:
#word order
ggplot() +
geom_polygon(data = map, aes(x = long, y = lat, group = group), fill="#dddddd") +
geom_point(data = df, aes(x = Longitude, y = Latitude, fill = factor(Word.order)), shape=21, size=3) +
theme_minimal()+
coord_sf()+
scale_fill_viridis_d(option = "plasma")+
labs(fill = "Word Order", y="",x="")
And since newbies sometimes use my tutorials: If you don’t understand something here, don’t give up, google it! Everyone does it, and most questions you might have, have already been asked and answered by someone somewhere.