Reputation: 125
(this is my first post so (i) I hope not to break too many rules and (ii) have to store example plots externally)
I would like to visualize irregular gridded timeseries data where the displayed parameter is also a function of a geographical measure like latitude or water depth. An example data file that contains the date (date), the geographical parameter water depth (dep) and the parameter of interest salinity (sal) and a preliminary scatterplot produced with ggplot2
are stored at our
password: timeseries
The R-code for the ggplot plot is:
# Load required packages
library(ggplot2)
library(data.table)
library(colorRamps)
library(scales)
# Import spatial timeseries data
df <- data.table(read.csv("timeseries_example.csv"))
df$date <- as.POSIXct(strptime(df$date, format="%m/%d/%Y", tz="GMT"))
# Scatterplot with color representing the z parameter
Fig <-
ggplot(data=df, aes(date, dep, col=Sal))+
geom_point()+
scale_y_reverse()+
scale_colour_gradientn(colours = matlab.like2(7), oob=squish)
tiff("./example_timeseries_R_ggplot.tiff", width = 200, height = 100,
units = 'mm', res = 300, compression = 'lzw')
Fig
dev.off()
As the data are spaced irregular in space and time, plotting with ggplot’s geom_tile()
function requires interpolation.
The freely available software ocean data view (ODV) enables such interpolation and I would like to reproduce the ODV plot also stored at our owncloud (link above) with R.
As this problem is similar to previously solved issues, I tried to interpolate the parameter sal on a finer grid of date and dep with the package akima
. However, this did not work with the x parameter being a POSIXct object.
Does anyone have a solution to this?
Upvotes: 3
Views: 3539
Reputation: 1101
I've had good luck with the MBA package:
# Load required packages
library(ggplot2)
library(lubridate)
library(reshape2)
library(colorRamps)
library(scales)
library(MBA)
# Import spatial timeseries data
df <- read.csv("timeseries_example.csv")
df$date <- as.POSIXct(strptime(df$date, format="%m/%d/%Y", tz="GMT"))
df$date <- decimal_date(df$date)
mba <- mba.surf(df[,c('date', 'dep', 'Sal')], 100, 100)
dimnames(mba$xyz.est$z) <- list(mba$xyz.est$x, mba$xyz.est$y)
df3 <- melt(mba$xyz.est$z, varnames = c('date', 'depth'), value.name = 'salinity')
Fig <-
ggplot(data=df3, aes(date, depth))+
geom_raster(aes(fill = salinity), interpolate = F, hjust = 0.5, vjust = 0.5) +
geom_contour(aes(z = salinity)) +
geom_point(data = df, aes(date, dep), colour = 'white') +
scale_y_reverse() +
scale_fill_gradientn(colours = matlab.like2(7))
Fig
There are some anomalies that you may be able to clean up with the with the interpolation settings. I just used the default.
Upvotes: 6