Problem plotting date in ggplot2

Below is the code I'm running but comes with error, I will include the error output below the actual code.
This is the code I run to keep the date column in date format

Convert the Date column to Date format

dataset$Date <- as.Date(dataset$Date, format = "%d-%m-%Y")

code to plot ggplot

library(ggplot2)
ggplot(dataset, aes(x = Date, y = Weekly_Sales)) +
  geom_line() +
  labs(title = "Weekly Sales Over Time",
       x = "Date",
       y = "Weekly Sales")

'Error in seq.int(0, to0 - from, by) : 'to' must be a finite number'
my understanding is this could happen if the dataset had any missing values, I did run another code to check if the dataset had any missing values, but it doesn't. I will include a link to the dataset 'Walmart Cleaned Data | Kaggle'

output from checking the dataset structure

dput(head(dataset))

structure(list(X = 0:5, Store = c(1L, 1L, 1L, 1L, 1L, 1L), Date = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"),
IsHoliday = c(0L, 0L, 0L, 0L, 0L, 0L), Dept = c(1, 26, 17,
45, 28, 79), Weekly_Sales = c(24924.5, 11737.12, 13223.76,
37.44, 1085.29, 46729.77), Temperature = c(42.31, 42.31,
42.31, 42.31, 42.31, 42.31), Fuel_Price = c(2.572, 2.572,
2.572, 2.572, 2.572, 2.572), MarkDown1 = c(0, 0, 0, 0, 0,
0), MarkDown2 = c(0, 0, 0, 0, 0, 0), MarkDown3 = c(0, 0,
0, 0, 0, 0), MarkDown4 = c(0, 0, 0, 0, 0, 0), MarkDown5 = c(0,
0, 0, 0, 0, 0), CPI = c(211.0963582, 211.0963582, 211.0963582,
211.0963582, 211.0963582, 211.0963582), Unemployment = c(8.106,
8.106, 8.106, 8.106, 8.106, 8.106), Type = c(3L, 3L, 3L,
3L, 3L, 3L), Size = c(151315L, 151315L, 151315L, 151315L,
151315L, 151315L)), row.names = c(NA, 6L), class = "data.frame")

All the Date values are NA, so there's no x variable to plot.

this is the output before running the code to change the date format

structure(list(X = 0:5, Store = c(1L, 1L, 1L, 1L, 1L, 1L), Date = c("2010-02-05",
"2010-02-05", "2010-02-05", "2010-02-05", "2010-02-05", "2010-02-05"
), IsHoliday = c(0L, 0L, 0L, 0L, 0L, 0L), Dept = c(1, 26, 17,
45, 28, 79), Weekly_Sales = c(24924.5, 11737.12, 13223.76, 37.44,
1085.29, 46729.77), Temperature = c(42.31, 42.31, 42.31, 42.31,
42.31, 42.31), Fuel_Price = c(2.572, 2.572, 2.572, 2.572, 2.572,
2.572), MarkDown1 = c(0, 0, 0, 0, 0, 0), MarkDown2 = c(0, 0,
0, 0, 0, 0), MarkDown3 = c(0, 0, 0, 0, 0, 0), MarkDown4 = c(0,
0, 0, 0, 0, 0), MarkDown5 = c(0, 0, 0, 0, 0, 0), CPI = c(211.0963582,
211.0963582, 211.0963582, 211.0963582, 211.0963582, 211.0963582
), Unemployment = c(8.106, 8.106, 8.106, 8.106, 8.106, 8.106),
Type = c(3L, 3L, 3L, 3L, 3L, 3L), Size = c(151315L, 151315L,
151315L, 151315L, 151315L, 151315L)), row.names = c(NA, 6L
), class = "data.frame")

the only explanation will be there's something wrong with the code I did run to convert the into Date format but can't figure out the issue

dataset$Date <- as.Date(dataset$Date, format = "%d-%m-%Y")

The dates are not in day-month-year format

2 Likes

Following alongfrom @dromano, in this case it will probably work if you leave off the format string.