Search code examples
rtime-seriesline-plot

Time-Series Data


I am trying to plot time-series data logged every hour in R. I want to plot Temp over time with my x-axis interval monthly. Currently plotting Temp against log number (61, 62, etc) and having trouble switching the x-axis to month.

`library(readr)
Apex_Log_Data <- read_csv("Aquaria/Apex Log Data.csv")
colnames(Apex_Log_Data)[16] <- "Salx2"
Apex_Log_Data[25] <- NULL
par(mfrow=c(2,1))
par(mar=c(4,2,1,1))
apex <- subset(Apex_Log_Data, Date!="NA")
plot(apex$Tmp, type = "l", ylim = c(25.5, 27.5), xlab = NA, ylab = NA)

Solution

  • Try this solution using lubridate for easy date handling, and ggplot() for very flexible plotting:

    # example data
    apex <- data.frame(ID = 60:64,
                         Date = c("9/1/18", "9/2/18", "10/1/18", "10/3/18", "11/2/18"),
                         Time = c("10:00:00", "11:00:00", "12:00:00", "1:00:00", "2:00:00"),
                         Tmp = c(27, 26.9, 26.9, 26.8, 26.8))
    
    
    
    library(ggplot2)
    library(lubridate)
    
    ggplot(apex) +
        geom_line(aes(x = mdy(Date), y = Tmp)) +
        ylim(c(25.5, 27.5)) +
        labs(x = "", y = "")
    

    enter image description here