Invalid input: date_trans works with objects of class Date only

Hello Community,

Hope all is well. When I run this code below, I receive the following error:
Error: Invalid input: date_trans works with objects of class Date only

When running line by line, it seems that the scales_x_date is not working properly.
Any help would be amazing. I will keep pushing, hopefully I find a break fingers crossed

library(tidyverse)
library(leaflet)
library(ggrepel)
library(ggplot2)
library(scales)

data <- read_csv("SFPD_Incident_Reports_2003_May2018.csv")

fil_calls <- data %>%
  filter(Category == "SUSPICIOUS OCC")

# mutating for annual year, and rounding time to nearest hour (24 hour clock, military time)
suspicious <- fil_calls %>%
  mutate(Year = format(as.Date(Date, "%m/%d/%Y"), format = "%Y")) %>% 
  mutate(Date = format(as.Date(Date, "%m/%d/%Y"))) %>% 
  mutate(Hour = format(strptime(Time, "%H:%M:%S"), '%H')) %>% 
  select(Hour, Year,Date, DayOfWeek, Category, Descript, PdDistrict, X, Y, Location)

# Calls over time
df_calls_daily <- suspicious %>%
  group_by(Date) %>% 
  summarise(count = n()) %>% 
  arrange(Date)

plot <- ggplot2::ggplot(data = df_calls_daily, aes(x = Date, y = count)) +
  geom_line(color = "#F2CA27", size = 0.1) +
  geom_smooth(color = "1A1A1A") +
  scale_x_date(breaks = date_breaks("1 year"), labels = date_format("%Y")) +
  labs(x = "Date of Call", y = "Number of Calls", title = "Daily Calls in San Francisco from 2003 - 2018")
print(plot)

Thanks for the almost complete reproducible example, called a reprex. It's missing one vital component, though, the source data. I've located it and am working on reproducing your error. I put some representative data in a gist

Another thing. Avoid using plot and other R reserved names. Use my_plot. For ggplot, I just use p <- ggplot(...)

Thank you for the advice.
I wanted to create a time lapse thing.

It is what I just created: the problem laid in these lines of code:

suspicious <- fil_calls %>%
  mutate(Year = format(as.Date(Date, "%m/%d/%Y"), format = "%Y")) %>% 
  mutate(Hour = format(strptime(Time, "%H:%M:%S"), '%H')) %>% 
  select(Hour, Year,Date, DayOfWeek, Category, Descript, PdDistrict, X, Y, Location)

and

suspicious <- fil_calls %>%
  mutate(Year = format(as.Date(Date, "%m/%d/%Y"), format = "%Y")) %>% 
  mutate(Date = format(as.Date(Date, "%m/%d/%Y"))) %>% 
  mutate(Hour = format(strptime(Time, "%H:%M:%S"), '%H')) %>% 
  select(Hour, Year,Date, DayOfWeek, Category, Descript, PdDistrict, X, Y, Location)

I had to properly convert the date from char to date. My old code was ineffective.
Then erased:

  mutate(Date = format(as.Date(Date, "%m/%d/%Y"))) %>% 

of code because it would overwrite the date and transform it back to a char.

I hopefully did a better job at breaking it down utilizing the reprex approach

1 Like

Great! Does that solve the question? If so, please mark it as a solution. Thanks.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.