1

I’m quite new to R and try to get started with plotting data. But I didn’t manage to get the x axis in the right order. It should be a time axis displaying the hours, starting with 00:00 to 23:59 with ticks every hour. I tried the axis.POSIXct() function, but it did not work. I really appreciate help. Thank you a lot in advance!

The code for the plotting was:

plot(V40VH_20171115_ACI$FILENAME, V40VH_20171115_ACI$MEAN, axes = FALSE, ylab = "ACI", xlab = "Hour")
axis(2, labels = TRUE)
box()

The code used for the other function:

axis.POSIXct(1, at=seq(from=as.POSIXct(strptime("00:00:00", format = "%H:%M:%S")), to=as.POSIXct(strptime("23:59:59", format = "%H:%M:%S")), by = 3600))

there is no error shown by R, but the function also don't change anything.

The current plot looks like this

ACI (missing timescale)

output of dput(droplevels(head(V40VH_20171115_ACI))) :

    structure(list(FILENAME = structure(1:6, .Label = c("audio-solo_red_V40-VH-2017-11-15_00-00-03_00000_000.wav", 
"audio-solo_red_V40-VH-2017-11-15_00-00-03_00059_997.wav", "audio-solo_red_V40-VH-2017-11-15_00-00-03_00119_995.wav", 
"audio-solo_red_V40-VH-2017-11-15_00-00-03_00179_992.wav", "audio-solo_red_V40-VH-2017-11-15_00-00-03_00239_990.wav", 
"audio-solo_red_V40-VH-2017-11-15_00-00-03_00299_987.wav"), class = "factor"), 
    SAMPLINGRATE = c(44100L, 44100L, 44100L, 44100L, 44100L, 
    44100L), BIT = c(16L, 16L, 16L, 16L, 16L, 16L), DURATION = c(60, 
    60, 60, 60, 60, 60), CHANNELS = c(2L, 2L, 2L, 2L, 2L, 2L), 
    INDEX = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "acoustic_complexity", class = "factor"), 
    FFT_W = c(512L, 512L, 512L, 512L, 512L, 512L), MIN_FREQ = c(200L, 
    200L, 200L, 200L, 200L, 200L), MAX_FREQ = c(6000L, 6000L, 
    6000L, 6000L, 6000L, 6000L), J = c(3L, 3L, 3L, 3L, 3L, 3L
    ), LEFT_CHANNEL = c(882.7496684, 865.6946215, 883.3736804, 
    875.5323086, 883.8283249, 860.3658252), RIGHT_CHANNEL = c(891.6367376, 
    889.7834472, 913.2662579, 910.2207809, 916.431259, 892.7898033
    ), MEAN = c(887.193203, 877.7390344, 898.3199692, 892.8765448, 
    900.1297919, 876.5778143)), .Names = c("FILENAME", "SAMPLINGRATE", 
"BIT", "DURATION", "CHANNELS", "INDEX", "FFT_W", "MIN_FREQ", 
"MAX_FREQ", "J", "LEFT_CHANNEL", "RIGHT_CHANNEL", "MEAN"), row.names = c(NA, 
6L), class = "data.frame")
Saskia
  • 23
  • 4
  • 2
    If you don't show code (in a minimal reproducible example) we can't tell you what you did wrong and how to fix it. Please read [How to create a minimal reproducible example](https://stackoverflow.com/a/5963610/1412059) and improve your post accordingly. – Roland May 22 '18 at 08:32
  • Seems like I can't edit my own question. The code for the plotting was: plot(V40VH_20171115_ACI$FILENAME, V40VH_20171115_ACI$MEAN, axes = FALSE, ylab = "ACI", xlab = "Hour") axis(2, labels = TRUE) box() The code used for the other function: axis.POSIXct(1, at=seq(from=as.POSIXct(strptime("00:00:00", format = "%H:%M:%S")), to=as.POSIXct(strptime("23:59:59", format = "%H:%M:%S")), by = 3600)) there is no error shown by R, but the function also don't change anything – Saskia May 22 '18 at 08:57
  • I'm pretty sure you can edit your own question. There is an *edit* link just above this comment thread. – Roland May 22 '18 at 08:58
  • yes, sorry. Now I see, I will add it to the question above. Then it is readable – Saskia May 22 '18 at 08:58
  • A datetime always consists of a date and a time and POSIXct is a datetime class. What you pass to axis (check it) probably does not have the same date as your data because `strptime` adds the current date if you don't specify. – Roland May 22 '18 at 09:24
  • I'm not really sure if I got it right. But even with using at=seq(from=as.POSIXct(strptime("2017-11-14 00:00:00", format = "%Y-%m-%d %H:%M:%S")), to=as.POSIXct(strptime("2017-11-14 23:59:59", format = "%Y-%m-%d %H:%M:%S")), by = 3600)) instead it does not work. my problem is that in some kind of way the date and the time is set in the name of the files used for the analysis, like "audio-solo_red_V40-VH-2017-11-14_00-00-02_00119_995.wav". There are usually in the first column of my data, but of course that does not work for labelling the x axis. – Saskia May 22 '18 at 11:04
  • Please add the output of `dput(droplevels(head(V40VH_20171115_ACI)))` to the question. – Roland May 22 '18 at 12:32
  • I added the information above – Saskia May 22 '18 at 19:40

2 Answers2

1

After you added a data sample, the main problem is obvious. Your x variable is not a datetime variable. You need to preprocess it. You also need to ensure that the breaks specified in axis have the same date as your data.

#extract datetime information:
V40VH_20171115_ACI$FILENAME <- substr(V40VH_20171115_ACI$FILENAME, 23, 47) 
#replace last underscore with dot
#read help("regex") and some regex tutorials to understand this
#see https://regexr.com/3psl6 for an explanation
V40VH_20171115_ACI$FILENAME <- gsub("_(?=\\d+$)", ".", 
                                    V40VH_20171115_ACI$FILENAME, perl = TRUE) 
#coerce to POSIXct with subsecond precision
V40VH_20171115_ACI$FILENAME <- as.POSIXct(V40VH_20171115_ACI$FILENAME, 
                                          format = "%Y-%m-%d_%H-%M-%OS", tz = "GMT")

#calculate breaks
start <- trunc(min(V40VH_20171115_ACI$FILENAME), "secs") #see help("round.POSIXt") for trunc options
                                                         #you probably want "days"
breaks <- seq(start, length.out = 10, by = 0.001) #adjust as needed

plot(V40VH_20171115_ACI$FILENAME, V40VH_20171115_ACI$MEAN, 
     axes = FALSE, ylab = "ACI", xlab = "Hour")
axis(2, labels = TRUE)
axis.POSIXct(1, at=breaks, format = "%H:%M:%OS3") #you probably want %H:%M

resulting plot

Note that there are issues with floating point numbers accuracy in millisecond POSIXct sequences; see the second axis break.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thank you a lot! I think I got now what's the problem with the data and the plotting. I will try your solution. Thank you! – Saskia May 24 '18 at 09:04
  • I adapted your solution a bit so that I can use hours to label the x axis, see my answer below if you're interested – Saskia May 25 '18 at 21:59
0

I can share my adapted version, it worked quite well now

#ACI
V40VH_20171114_ACI$TIME <- seq(from=as.POSIXct(strptime("2017-11-14 00:00:00", format = "%Y-%m-%d %H:%M:%S")), to=as.POSIXct(strptime("2017-11-14 23:59:00", format = "%Y-%m-%d %H:%M:%S")), by=60)
plot(V40VH_20171114_ACI$TIME, V40VH_20171114_ACI$MEAN, axes = FALSE, ylab = "ACI", xlab = "Hour", pch = 16, cex = 0.05)
axis(2, labels = TRUE)
axis.POSIXct(1, at=seq(from=as.POSIXct(strptime("2017-11-14 00:00:00", format = "%Y-%m-%d %H:%M:%S")), to=as.POSIXct(strptime("2017-11-14 24:00:00", format = "%Y-%m-%d %H:%M:%S")), by = 3600))
box()

ACIwithttimescale

Saskia
  • 23
  • 4