Thiago V. dos Santos
2014-Sep-10 20:20 UTC
[R] How to plot soil moisture data as a contour plot
Dear all,
This is my first message in this list, so please excuse any mistake.
I am trying to plot moisture data for 11 soil layers over the course of the
year, but I am yet to find the correct function to do that. I am trying to
reproduce the lower figure in this panel: https://imageshack.com/i/exmVz5QSp
Please read the comments while reproducing my data with the code below:
----------------------------------------
library(repmis) # reads text data directly from dropbox - no need to download
any file
# read data
url <- 'https://dl.dropboxusercontent.com/u/27700634/precip.txt'
tmp <- repmis::source_data(url, sep = '', header = TRUE)
# convert julian day to date
date <- as.Date(tmp$julian, origin='2011-12-31')
data <- cbind(date, tmp)
head(data)
# now, convert soil layers to matrix and transpose it
mat <- t(as.matrix(data[, 4:14]))
head(mat)
# this is the very matrix I want to plot. Please notice that it is already
organized as a "profile",
# with rows representing soil layers and columns representing day of year.
----------------------------------------
My first attempt was to use function filled.contour from "graphics"
package. I define a vector with labels for soil layers and then I try to plot,
but I receive an error saying the matrix has incorrect dimensions:
----------------------------------------
# define vector with depth of soil layers
depths <- c(0.05,0.10,0.20,0.30,
0.40,0.60,0.80,1.00,
1.50,2.00,2.50)
# Plot soil moisture profile
plot <- filled.contour(data$julian, depths, mat)
#Error in .filled.contour(x, y, z, levels, col) : dimension mismatch
----------------------------------------
I can obviously tranpose the matrix to force the plot, but the resulting figure
is not what I need - the soil profile is shown upside down:
----------------------------------------
plot <- filled.contour(data$julian, depths, t(mat))
----------------------------------------
Because this functions requires axis labels to be ascendent, I am not able to
reverse them in order to show the first layer at the top of the graphic.
I would really appreciate any feedback or directions on how to show my data as a
contour plot as mentioned above. Thanks in advance!
Greetings,
--
Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm
Phone: (612) 323
9898
[[alternative HTML version deleted]]
David Winsemius
2014-Sep-10 21:56 UTC
[R] How to plot soil moisture data as a contour plot
On Sep 10, 2014, at 1:20 PM, Thiago V. dos Santos wrote:> Dear all, > > This is my first message in this list, so please excuse any mistake. > > I am trying to plot moisture data for 11 soil layers over the course of the year, but I am yet to find the correct function to do that. I am trying to reproduce the lower figure in this panel: https://imageshack.com/i/exmVz5QSp > > Please read the comments while reproducing my data with the code below: > > ---------------------------------------- > library(repmis) # reads text data directly from dropbox - no need to download any file > > # read data > url <- 'https://dl.dropboxusercontent.com/u/27700634/precip.txt' > tmp <- repmis::source_data(url, sep = '', header = TRUE) > > # convert julian day to date > date <- as.Date(tmp$julian, origin='2011-12-31') > data <- cbind(date, tmp) > head(data) > > # now, convert soil layers to matrix and transpose it > mat <- t(as.matrix(data[, 4:14])) > head(mat) > > # this is the very matrix I want to plot. Please notice that it is already organized as a "profile", > # with rows representing soil layers and columns representing day of year. > ---------------------------------------- > > > My first attempt was to use function filled.contour from "graphics" package. I define a vector with labels for soil layers and then I try to plot, but I receive an error saying the matrix has incorrect dimensions: > > > ---------------------------------------- > > # define vector with depth of soil layers > > depths <- c(0.05,0.10,0.20,0.30, > 0.40,0.60,0.80,1.00, > 1.50,2.00,2.50) > > # Plot soil moisture profile > plot <- filled.contour(data$julian, depths, mat) > > > #Error in .filled.contour(x, y, z, levels, col) : dimension mismatchYou need to decide what orientation you want. At the moment the mat-matrix is 11 x 366 and the x-component is the 11. (And for some reason you are the one who made the first transposition.) There's no auto-transformation in filled.contour when the dimensions are reversed. This proceeds without error: mat2 <- as.matrix(data[, 4:14]) plot <- filled.contour(x=data$julian, y=depths, z=mat2)> ---------------------------------------- > > > I can obviously tranpose the matrix to force the plot, but the resulting figure is not what I need - the soil profile is shown upside down: >If you want the reverse the depth order, do not transpose, .... just: plot <- filled.contour(x=data$julian, y=depths, z=mat2[, 11:1])> ---------------------------------------- > plot <- filled.contour(data$julian, depths, t(mat)) > > ---------------------------------------- > > > Because this functions requires axis labels to be ascendent, I am not able to reverse them in order to show the first layer at the top of the graphic. > > I would really appreciate any feedback or directions on how to show my data as a contour plot as mentioned above. Thanks in advance! > > > Greetings, > -- > Thiago V. dos Santos > PhD student > Land and Atmospheric Science > University of Minnesota > http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm > Phone: (612) 323 > 9898 > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA