ICT International

Advancing soil, plant and environmental decision making

Using R to import and analyse SFM1x Sap Flow Meter data

OVERVIEW

Widely used by the research community, R is a common tool used in many scientific research publications. Recognising this, ICT International has produced a series of R code examples that can be used by researchers to manage the import and organisation of their SFM1 Sap Flow data in CSV format. Whilst researchers are often familiar with importing CSV data into R, there are some peculiarities that need to be addressed. In time, these code examples will be expanded to include the option to import JSON files.

PRODUCTS

No products found

LOCATION

In turn, the options for accessing and setting the data up for analysis will be explained in the tabs.

  • CATM1 tab is for access via the ICT International CATM1 server
  • SD Card Access is for downloaded CSV files stored on your own computer (or at a file location that R can access).
  • Finally, the Visualisation tab provides some examples of visualisation options within R.

To initially set up R, it is as always necessary to download and install the packages. Using common packages that many users are familiar with the following packages are used here:

Packages Used

To set up the import, there are several packages used in the import, handling and preliminary analysis. The following code can be used to undertake the installation of packages used:

#General Import
#readr for importing CSV files
install.packages("readr")
library(readr)
#dplyr: additional functionality
install.packages("dplyr")
library(dplyr)
#Lubridate to manage the date/time formatting
install.packages("lubridate")
library(lubridate)
For data visualisation:
#ggplot2: install and load ggplot2 for graphing and analytics (or package of your choice)
install.packages("ggplot2")
library(ggplot2)
#Plotly: 3d time series diagrams
install.packages("plotly")
library(plotly)
#magrittr required by plotly and other lines within the script where the pipe function is used
#install.packages("magrittr")
library(magrittr)

For optional geolocation:

#Geolocation: Using leaflet to produce the maps
install.packages("leaflet")
library(leaflet)

Irrespective of the method used to access data, the visualisation follows the same options for either option

Data Visualisation

With common data visualisation tools, it is possible to present the data from the Sap Flow Meter for analysis.

CATM1 Data

#ggplot for a point and line graph

Uncorrected_in_SFM001 <- ggplot(SFM_001, aes(x=Date_Time, y=Uncorrected_In_cmphr)) + geom_line() + geom_point()

Uncorrected_in_SFM001

jpeg(‘Uncorrect_in_SFM001.png’, width=1800, height=800, units = “px”)

Uncorrected_in_SFM001

dev.off()

Using Plotly, it is possible to present a 3D graph as well:

#plotly for a 3D graph

plot_ly(data = SFM_001, x=SFM_001$Date_Time, y=SFM_001$Uncorrected_In_cmphr, z=SFM_001$Uncorrected_Out_cmphr, intensity = ~Uncorrected_In_cmphr, colorscale = list(c(0,’red’), c(0.33,’orange’), c(0.66, ‘yellow’), c(1, ‘green’)), type=”mesh3d” )

Locally Stored Data (CSV from SD Card or BlueTooth)

#ggplot

Uncorrected_in_SFMcsv <- ggplot(SFM_csv, aes(x=Date_Time, y=Uncorrected_In_cmphr)) + geom_line() + geom_point()

Uncorrected_in_SFMcsv

jpeg(‘Uncorrect_in_SFMcsv.png’,  width=1800, height=800, units = “px”)

Uncorrected_in_SFMcsv

dev.off()

SFM_csv$Uncorrected

For 3D visualisation, the following code is used:

#plotly

plot_ly(data = SFM_csv,

x=SFM_csv$Date_Time,

y=SFM_csv$Uncorrected_In_cmphr,

z=SFM_csv$Uncorrected_Out_cmphr,

intensity = ~Uncorrected_In_cmphr,

colorscale = list(c(0,’red’),

c(0.33,’orange’),

c(0.66, ‘yellow’),

c(1, ‘green’)),

type=”mesh3d” )

Geographical Visualisation

With the coordinates added to the data in the previous stages, it is possible to add maps to display sensor location:

Separate Maps

For separate maps, each map can be built individually

#CATM1 Map

SFM_001_map <- leaflet()

SFM_001_map <- addTiles(SFM_001_map)

SFM_001_map <- addMarkers(SFM_001_map,SFM_01_Longitude, SFM_01_Latitude, popup=”SFM_010 Site”)

SFM_001_map

#CSV Map

SFM_csv_map <- leaflet()

SFM_csv_map <- addTiles(SFM_csv_map)

SFM_csv_map <- addMarkers(SFM_csv_map,SFM_CSV_Longitude, SFM_CSV_Latitude, popup=”SFM_CSV Site”)

SFM_csv_map

Combined Maps

If you want to combine the sensor locations on a single map, then the following code can be used:

SFM_map <- leaflet()

SFM_map <- addTiles(SFM_map)

SFM_map <- addMarkers(SFM_map,SFM_01_Longitude, SFM_01_Latitude, popup=”SFM_010 Site”)

SFM_map <- addMarkers(SFM_map,SFM_CSV_Longitude, SFM_CSV_Latitude, popup=”SFM_CSV Site”)

SFM_map