0

I am analyzing day to day data to see when the value would be lower. I set each day as categorical variable so I can differentiate each day. But I want to get each day plotted on top of another day instead of one continuous graph as shown below.

Plot

Data set:

                    Value        Day
2013-01-03 01:55:00 0.35435715    1
2013-01-03 02:00:00 0.33018654    1
2013-01-03 02:05:00 0.38976118    1
2013-01-04 02:10:00 0.45583868    2
2013-01-04 02:15:00 0.29290860    2

My current ggplot code is as follows:

g <- ggplot(data = Data, aes(x = Index, color = Dates)) +
    geom_line(y = Data$Value) +
    scale_x_datetime(date_breaks = TimeIntervalForGraph, date_labels = "%H") +
    xlab("Time") +
    ylab("Random value")

I would really appreciate if anyone can guide me on how I can turn my x-axis into 24hrs time series so that I can plot each day on the same graph to see when the value is lower during the 24 hrs.Thanks in advance.

Method tried: I tried creating an 3rd column with time only, for some reasons the following codes didnt work:

time <- format(index(x), format = "%H:%M"))
data <- cbind(data, time)
boniface316
  • 489
  • 3
  • 17

2 Answers2

1

Please try the following and let me know if it works (here I am taking tm time column as given):

Data$tm = strftime(Data$tm, format="%H:%M:%S")

library(ggplot2)
ggplot(Data, aes(x = tm, y = Value, group = Day, colour = Day)) +
  geom_line() +
  theme_classic()
Axeman
  • 32,068
  • 8
  • 81
  • 94
AK88
  • 2,946
  • 2
  • 12
  • 31
  • I got all NA with the following error message: Warning message: In merge.xts(..., all = all, fill = fill, suffixes = suffixes) : NAs introduced by coercion – boniface316 Aug 14 '17 at 11:07
  • can you post your data using `dput()`? – AK88 Aug 14 '17 at 11:56
  • as requested. structure(c(1357189200, 1357189500, 1357189800.....1357620600, 1357620900), tzone = "America/New_York", tclass = c("POSIXct", "POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "America/New_York", tclass = c("POSIXct", "POSIXt"), tzone = "America/New_York", class = c("xts", "zoo")) – boniface316 Aug 14 '17 at 23:43
1

You need a way of summarising the data for each hour of the day. Here are some approaches you're probably looking for:

library(xts)
library(data.table)
library(ggplot2)

tm <- seq(as.POSIXct("2017-08-08 17:30:00"), by = "5 mins", length.out = 10000)
z <- xts(runif(10000), tm, dimnames = list(NULL, "vals"))

DT <- data.table(time = index(z), coredata(z))
# note the data.table syntax is different:
DT[, hr := hour(time)]

# Plot the average value by hour:
datByHour <- DT[, list(avgval = mean(vals)), by = c("hr")]

# Use line plot if you have one point per hour:
g <- ggplot(data = datByHour, aes(x = hr, y = avgval, colour = avgval)) +
    geom_line()


datByHour <- DT[, list(avgval = mean(vals)), by = c("hr")]
# visualise the distribution by hour:
g2 <- ggplot(data = DT, aes(x = hr, y = vals, group = hr)) +
    geom_boxplot()
FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67