I am trying to create a scatter plot with data from FRED. Whenever I run the program, I get back the "Error: invalid input: date_trans works with objects of class Date only".
fredsym <- c("UNRATE","JTSJOR")
for (i in 1:length(fredsym)){
getSymbols(fredsym[i],src="FRED")
}
Creating a dataframe for the UNRATE and JTSJOR
unemployment_diff <- diff(UNRATE)
unemployment_diff_bar.df = data.frame(date=time(unemployment_diff), coredata(unemployment_diff))
job_vacancy_diff <- diff(JTSJOR)
job_vacancy_diff_bar.df = data.frame(date=time(job_vacancy_diff), coredata(job_vacancy_diff))
Merging unemployment and job vacancy dataframes to a single dataframe
beveridge_curve <- merge(unemployment_diff_bar.df, job_vacancy_diff_bar.df, all=FALSE)
Plotting
scatter__plot <- ggplot(beveridge_curve, aes(x=UNRATE, y=JTSJOR)) geom_point()
stat_cor(label.x=12, label.y=7)
labs(title = "Beverridge Curve", x = NULL,y = NULL, caption = "Source:BLS")
scale_y_continuous(limits=c(-500,1500), breaks=seq(-500,1500,200), labels = comma)
theme_bw()
theme (legend.title=element_blank(), legend.position = c(0.8, 0.9),
axis.text.x = element_text(size = 14, angle = 45, hjust=1),
axis.text.y = element_text(size = 14),
axis.title = element_text(size = 14),
plot.title = element_text(face="plain", size = 20, hjust=0.5, color="#3333B3FF"),
panel.border = element_blank())
scale_x_date(labels = date_format("%Y-%m"), breaks='1 month')
CodePudding user response:
You do not need to create dataframes for the plot. Simply merge unemployment_diff and job_vacancy_diff and remove the scale_x_date line.
This is because the Beveridge curve doe not need dates, only UNRATE and JTSJOR data values.
CodePudding user response:
This error occurred because you have not assigned a date column for the x axis. You are using UNRATE
column for the x axis, which are not in date format