Welcome to TERN Knowledge Base
Visualising site locations using GeoJSON data with RStudio
GeoJSON data can be parsed using R to create a plot that visualises the geographic range of your TERN EcoPlots dataset. The GeoJSON data that is included in TERN data package downloads can be parsed immediately to create a plot that visualises the geographic range of your TERN EcoPlots dataset. Below is an example of such a basic plot.
Below is the script used to created the above plot. It contains code comments to give a basic explanation of each element of the plot creation process.
Setup
Before running the script in R or RStudio, there are a number of setup steps needed.
Download some TERN GeoJSON data. This example uses a dataset acquired from data search results in Example Data Searches in EcoPlots | Example Two Plants in Western Australia.
Set your R working directory to an appropriate location on your device.
Copy your GeoJSON file to your R working directory and rename if desired.
Install the R packages that are called in the first code chunk.
# R package libraries required
library(geojsonio)
library(tidyverse)
library(stringr)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
# read in data from the GeoJSON file
plants <- geojson_read("geojson_PlantsInWA.json")
# some general exploration of our data
View(plants)
summary(plants)
str(plants$features)
# Visualisation preparation tasks
# Transform GoeJSON data into a dataframe named plant_polys
plant_polys <-
lapply(1:length(plants$features),
function(i){
tmpdata <- plants$features[[i]]$geometry$coordinates[[1]][1] %>%
data.frame() %>%
rename('longitude'='.') %>%
mutate(latitude= plants$features[[i]]$geometry$coordinates[[2]][1]) %>%
tibble %>%
mutate(siteName = plants$features[[i]]$properties$siteName)
}) %>% bind_rows()
# Have a look at how the dataframe is structured
str(plant_polys)
# Specifically every row
print(plant_polys, n=40)
# Some site names contain a comma then further information.
# This will make the plot site names look a bit messy.
# OPTIONAL - truncate title to look neater on the plot
shortSiteName <- sapply(str_split(plant_polys$siteName,",",),'[',1)
# import world country polygons from the rnaturalearth package
world <- ne_countries(scale = "large", returnclass = "sf")
# Create the visualisation
# Draw a plot from the dataframe
# using the world country polygons as the background
ggplot(data = world) +
geom_sf() +
geom_point(data = plant_polys, aes(x = longitude, y = latitude), size = 1,
shape = 23, fill = "darkgreen") +
coord_sf(xlim=c(114,124), ylim=c(-36,-29), expand = FALSE) +
labs(x = 'Longitude', y = 'Latitude',
title = "TERN Plots with identified plant species data in Western Australia") +
annotate("label",
x = plant_polys$longitude + 0.7,
y = plant_polys$latitude + 0.25,
label = shortSiteName,
size = 3.5,
color = "darkgreen"
)
The final step is to export your plot image using the “Export” option from the plot viewer or the Plots menu.
This is a basic example of one of the many ways that GeoJSON data can be visualised using R. We recommend visiting sites such as The R Graph Gallery for ideas on ways to customise the aesthetics of your plot.
Provide your feedback about the experience with Knowledge base