r - Issue with as.POSIXct values on timeseries plot -
i having issue x axis on time series graph trying put together. have 4 separate data sets 2000 observations in each one. how of data sets formatted:
> str(w) 'data.frame': 2116 obs. of 6 variables: $ time.stamp : factor w/ 2116 levels "11/11/2016\t00:00:00 am",..: 1 2 3 4 5 6 7 8 9 10 ... $ date : date, format: "2016-11-11" "2016-11-11" ... $ time : factor w/ 727 levels "00:00:00","00:02:00",..: 1 2 3 4 5 6 8 11 12 13 ... $ value : num 1.4 1.39 1.41 1.4 1.41 1.4 1.39 1.4 1.37 1.39 ... $ state.of.value: factor w/ 1 level "200 (unchecked)": 1 1 1 1 1 1 1 1 1 1 ... $ datetime : posixct, format: "2016-11-11 00:00:00" "2016-11-11 00:02:00"
and here code using graph 4 parameters on same graph:
w <- read.csv("w.csv") x <- read.csv("x.csv") y <- read.csv("y.csv") z <- read.csv("z.csv") #creating date time column each parameter w$datetime <- paste(w$date, w$time, sep = "") x$datetime <- paste(x$date, x$time, sep = "") y$datetime <- paste(y$date, y$time, sep = "") z$datetime <- paste(z$date, z$time, sep = "") #making r recognize date time column actual date time w$datetime <- strptime(w$datetime, "%m/%d/%y %h:%m:%s") w$datetime <- as.posixct(w$datetime) x$datetime <- strptime(x$datetime, "%m/%d/%y %h:%m:%s") y$datetime <- strptime(y$datetime, "%m/%d/%y %h:%m:%s") z$datetime <- strptime(z$datetime, "%m/%d/%y %h:%m:%s") #changing date column actual date w$date <- as.date(w$date, "%m/%d/%y") x$date <- as.date(x$date, "%m/%d/%y") y$date <- as.date(y$date, "%m/%d/%y") z$date <- as.date(z$date, "%m/%d/%y") #plotting of these par(mfrow = c(1,1)) par(mar = c(5, 7, 4, 4)) plot(w$datetime, w$value, axes = t, ylim = c(0, 4), xlab = "datetime", #xaxt = "n", ylab = "", type = "l", col = "red", lwd = 2) axis(2, ylim = c(0, 4), lwd = 2, col = "black") #axis(1, xlim = c(w$date[1], w$date[n]), lwd = 0, col = "black", labels = xlabels, @ = (w$datetime == xlabels)) mtext(2, text = "w/x", line = 2, col = "black") points(x$datetime, x$value, pch = 20, col = "blue", type = "l", lwd = 2) par(new = t) plot(y$datetime, y$value, axes = f, ylim = c(400, 750), ylab = "", xlab = "", type = "l", lty = 1, main = "", lwd = 2, col = "mediumorchid1") axis(2, lwd = 2, line = 3.5, col = "black") mtext(2, text = "y", line = 5.5, col = "black") par(new = t) plot(z$datetime, z$value, axes = f, ylim = c(0, 25), xlab = "", ylab = "", type = "l", lty = 1, main = "", col = "purple", lwd = 2) axis(4, ylim = c(0, max(z$value)+15), lwd = 2, col = "black") mtext(4, text = "z", line = 2, col = "black") legend("topleft", legend = c("w", "x", "y", "z"), col = c("red", "blue", "mediumorchid1", "purple"), lty = 1, lwd = 2)
this graph produces: as.posixct date issue
so, issue on x axis, no matter try can't list datetime datetime (formatted either "%m-%d-%y" or "%m-%d-%y %h:%m:%s") , continues list "fri", "sat",... etc. seems happen when data spans few days in instance. have tried several different date formatting removing initial x axis plot() function , adding in axis() function after it. when try that, have issues placing tick marks though since w$datetime has length of 2116.
as side note, unable post datasets has encountered issue before.
any suggestions appreciated!
update: able around problem using labels =
, at=
arguments within axis()
function.
by creating 2 data frames, able put desired labels @ each tick mark:
xlabels <- c("11-11-2016", "11-12-2016", "11-13-2016", "11-14-2016") xlabels1 <- c("11-11-2016 12:00 am", "11-12-2016 12:00 am", "11-13-2016 12:00 am", "11-14-2016 12:00 am") xlabels <- strptime(xlabels, "%m-%d-%y") xlabels <- as.posixct(xlabels) #plotting of these par(mfrow = c(1,1)) par(mar = c(5, 7, 4, 4)) plot(w$datetime, w$value, axes = t, ylim = c(0, 4), xlab = "datetime", xaxt = "n", ylab = "", type = "l", col = "red", lwd = 2) axis(2, ylim = c(0, 4), lwd = 2, col = "black") axis(1, xlim = c(w$date[1], w$date[n]), lwd = 0, col = "black", labels = xlabels1, @ = xlabels) mtext(2, text = "w/x", line = 2, col = "black") points(x$datetime, x$value, pch = 20, col = "blue", type = "l", lwd = 2) par(new = t) plot(y$datetime, y$value, axes = f, ylim = c(400, 750), ylab = "", xlab = "", type = "l", lty = 1, main = "", lwd = 2, col = "mediumorchid1") axis(2, lwd = 2, line = 3.5, col = "black") mtext(2, text = "y", line = 5.5, col = "black") par(new = t) plot(z$datetime, z$value, axes = f, ylim = c(0, 25), xlab = "", ylab = "", type = "l", lty = 1, main = "", col = "purple", lwd = 2) axis(4, ylim = c(0, max(z$value)+15), lwd = 2, col = "black") mtext(4, text = "z", line = 2, col = "black") legend("topleft", legend = c("w", "x", "y", "z"), col = c("red", "blue", "mediumorchid1", "purple"), lty = 1, lwd = 2)
Comments
Post a Comment