Help with #1941
Unanswered
rvaillancourt
asked this question in
Q&A
Help with
#1941
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am having difficulty creating a contour plot that accurately reflects the real data. I am starting with a *.csv file from an ocean glider that travels in zig-zag up-and -down movements near the edge of the continental shelf and collects data on water depth and temperature and distance. The actual data are shown in the first plot using the scatter2D function in the Plot3D package. You can see from this that as the glider moves from left to right it is first in water ca. 800 meters deep and then as it moves onshore it moves into water 200 meters deep. The next plot is the contour plot created by plotly. For some reason it is extrapolating to spatial domains that are outside the bounds of the real data, giving temperature values at 800 meters on the right hand side of the graph.
My question is, "How can I contour plot these data in a realistic way that does not lead to this over-extrapolation?"
The R script is as follows:
R script start*********************************************************************
#This is a practice script using oceanographic data
#collected by a Webb Slocum glider
library(tidyverse)
library(plotly)
#read in new file
track1 <- read_csv("track1.csv")
#create new input file, "track1a.csv" with missing data removed
track1a <- na.omit(track1)
#interpolation
s <- interp(track1a$distance, track1a$depth*-1, track1a$temperature, duplicate="mean",
xo=seq(min(track1a$distance), max(track1a$distance), length=20),
yo=seq(min(track1a$depth*-1), max(track1a$depth*-1), length=20))
filled.contour(s, color = terrain.colors, ylim = range(depth, -800, 0))
#create scatterplot of glider transect for temperature
library(plot3D)
scatter2D(track1$distance, track1$depth*-1, colvar = track1$temperature,
pch = ".", cex = 2, colkey = list(width = 0.5, length = 0.75, side = 4),
ylim = NULL,
xlab = "distance along transect, m", ylab = "Depth, m",
clab = c(".", "Degrees C"), main = "Glider", cex.axis = 1.25,
cex.main = 1.5, cex.sub = 1.5, cex.lab = 1.1 )
#Perform Loess regression for smoothing to create matrix for contour plot
z <- track1$temperature
x <- track1$distance
y <- track1$depth*-1 #depth in reverse
data.loess <- loess(z ~ x*y, data = track1, span = 0.5)
Create a sequence of incrementally increasing (by 1% of range increments) values for both distance and depth
xgrid <- seq(min(x), max(x), 5)
ygrid <- seq(min(y), max(y), 5)
Generate a dataframe with every possible combination of distance and depth
data.fit <- expand.grid(x = xgrid, y = ygrid)
Feed the dataframe into the loess model and receive a matrix output with estimates of
temperature for each combination of distance and depth
mtrx3d <- predict(data.loess, newdata = data.fit)
Abbreviated display of final matrix
mtrx3d[1:10, 1:10]
y
#x y=-119.708641 y=-118.438641 y=-117.168641 y=-115.898641 y=-114.628641 y=-113.358641
#x=40.15091 13.37925 13.34699 13.31249 13.27583 13.23707 13.19628
#x=40.15291 13.37282 13.34054 13.30589 13.26896 13.22982 13.18857
#x=40.15491 13.36601 13.33383 13.29915 13.26209 13.22273 13.18117
#x=40.15691 13.35881 13.32685 13.29229 13.25523 13.21578 13.17406
#x=40.15891 13.35126 13.31964 13.28530 13.24837 13.20897 13.16723
Transform data to long form
mtrx.melt <- melt(mtrx3d, id.vars = c('distance', 'depth'), measure.vars = 'temperature')
names(mtrx.melt) <- c('distance', 'depth', 'temperature')
Return data to numeric form
mtrx.melt$distance <- as.numeric(str_sub(mtrx.melt$distance, str_locate(mtrx.melt$distance, '=')[1,1] + 1))
mtrx.melt$depth <- as.numeric(str_sub(mtrx.melt$depth, str_locate(mtrx.melt$depth, '=')[1,1] + 1))
fig1 <- plot_ly(mtrx.melt, x = ~distance, y = ~depth, z = ~temperature, type = "contour",
width = 600, height = 500, contours = list(showlabels = TRUE),contours = list(
end = 15,
size = 0.2,
start = 5
),
line = list(smoothing = 0), ncontours = 20)
fig1
end R script*********************************************
The following are the plot files. The input file is "track1.zip
track1.zip
".
Beta Was this translation helpful? Give feedback.
All reactions