简体   繁体   中英

Create a filled.contour plot with time (POSIXct) on x axis

I want to make a plot with time on x axis, depth on y axis and temperature as z values. I converted time into the posixct format:

DateTime<-as.character.Date(Rohdaten$Datum..UTC.)
CompleteDate <- as.POSIXct(DateTime, format = "%d.%m.%Y %H:%M", tz = "UTC")

These are the depths:

Tiefen <- c(1, 3, 5, 10, 15, 20.5)

And my temperature values are in a matrix:

Temp <- as.matrix(data.frame(Daten$D1.1m, Daten$D1.3m, Daten$D1.5m, Daten$D1.10m, Daten$D1.15m, Daten$D1.20.5m))
Temp2 <- matrix(Temp, ncol=ncol(Temp), dimnames = NULL)

I use this code to make the plot:

filled.contour(CompleteDate,Tiefen,Temp2)

But I get an error message:

Error in filled.contour(CompleteDate, Tiefen, Temp2) : 
ansteigende 'x' und 'y' Werte erwartet

This error says that filled.contour expects ascending x and y values. When I take

Time <- c(1:3405)

Instead of the time in the posixct format the plot is created:

Plot1

What can I do to take my actual dates and times as the x axis?

Here are my CompleteDates in the POSIXct format: https://www.dropbox.com/s/f0h643u7lpzq9mu/CompleteDate.txt?dl=0

Here is a working example using only a snapshot of your Dates and fictional Temperatures:

DateTime <- structure(c(1472209200, 1472212800, 1472216400, 1472220000, 1472223600, 
                        1472227200, 1472230800, 1472234400, 1472238000, 1472241600, 1472245200, 
                        1472248800, 1472252400, 1472256000, 1472259600, 1472263200, 1472266800, 
                        1472270400, 1472274000, 1472277600, 1472281200, 1472284800, 1472288400, 
                        1472292000, 1472295600, 1472299200, 1472302800, 1472306400, 1472310000, 
                        1472313600, 1472317200, 1472320800, 1472324400, 1472328000, 1472331600
), class = c("POSIXct", "POSIXt"), tzone = "UTC")

Tiefen <- c(1, 3, 5, 10, 15, 20.5)

# now create fictive temperatures
df   <- expand.grid("Date" = DateTime, "Tiefen" = Tiefen)
df   <- df %>% group_by(Tiefen) %>% mutate(Temp = runif(n(), 15 - Tiefen/2, 27 - Tiefen)) %>% ungroup()
Temp <- matrix(df$Temp, nrow = length(DateTime))


inMeter <- function(x) {
  paste0(x, "m")
}

inCelsius <- function(x) {
  paste0(x, "°C")
}

filled.contour(x = as.numeric(DateTime), y = Tiefen, z = Temp,  
               plot.axes = { 
                 axis(1, labels = DateTime, at = as.numeric(DateTime), las = 2, cex.axis = 0.5) 
                 axis(2, at = Tiefen, labels = inMeter(Tiefen), cex.axis = 1) 
               },
               key.axes = {
                 axis(4, at = pretty(Temp), labels = inCelsius(pretty(Temp))) 
               })

Notes:

  • We supply the unix timestamp (by using as.numeric() ) as x values for the plot.
  • We plot a new axis using the original POSIXct strings. I made them smaller. Since you have a lot of dates, make sure the plot is not overloaded.
  • The z values ( Temp ) are supplied as a matrix which has as many rows as we have x values.
  • I added two functions in order two plot the y axis for both the plot and the key with corresponding units.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM