esc1234
esc1234

Reputation: 23

Plotting a time series graph with multiple variables representing date in a data frame

I am trying to plot a time series type graph with dates on x-axis and mean temperature values on the y-axis. The data frame I'm working from has two separate variables representing date:

year contains integer values e.g., 1941, 1942 etc.
month is a factor variable with 12 levels containging 'January', 'February'...'December'

Here's a clip of a the dataset using dput, sorry don't know how to format it to be able to view it here.

structure(list(year = c(1941L, 1941L, 1942L, 1942L, 1942L), month = structure(c(11L, 
12L, 1L, 2L, 3L), .Label = c("January", "February", "March", 
"April", "May", "June", "July", "August", "September", "October", 
"November", "December"), class = "factor"), meant = c(6.9, 6.5, 
4.3, 2.9, 6.3)), row.names = c(NA, 5L), class = c("WeatherData", 
"data.frame"))

So far I have tried converting year and month to character variables, pasting the two character variables to a single character variable yrmonth and then converting this variable from character type to date type so that the plot() function can plot the dates in the correct format.

# convert integer and factor variable to character variable
df$month <- as.character(df$month)
df$year <- as.character(df$year)

# for loop to paste two character variables to yrmonth
yrmonth <- vector(length = length(df$year))
for(i in 1:length(df$year)){
  yrmonth[i] <- (paste(df$month[i], df$year[i]))
}
df$yrmonth <- yrmonth

# convert yrmonth to date format
df$yrmonth  <- parse_date_time(yrmonth, orders = c('bdy', 'bY' ))

plot(df$yrmonth, df$meant, type = 'l')

as df$meant is a variable with integer values I am hoping to produce a time series graph. When I execute the code above I get an error error in plot.window(...) : Need finite 'xlim' values

Can anyone suggest any way I can produce a graph given the characteristics of my date variables?

Upvotes: 1

Views: 393

Answers (1)

Niels Holst
Niels Holst

Reputation: 626

Assuming you use the lubridate package, you can construct dates a lot easier than that:

df$date = paste(rep(15, nrow(df)), df$month, df$year, sep="/")
# check that date column contains valid dates as strings
df$date = dmy(df$date)
# check that dates have been converted correctly

To get valid dates, I set date to the 15th of each month.

Upvotes: 0

Related Questions