I'm trying to plot multiple time series plots based on different datasets. The first data set plotted perfectly.
library(tidyverse)
library(dplyr)
data("economics")
economics %>%
ggplot(aes(date, unemploy, pop))
geom_line(col = "maroon")
xlab("Year")
ylab("U.S. Unemployment Rate")
The second data set may need some additional conditioning, but essentially it's showing the same type of data, but isn't plotting the same. The data can be found here
What should I be doing differently?
CodePudding user response:
The problem here is that read.csv
reads the column Record.Date
as type character, by default. ggplot
then interprets the date variable as a factor, whereas you wanted a date type on the x-axis.
You can solve this in a few ways.
Use
readr::read_csv
. In this case the column will be read as type date because it's in a standard "Year-Month-Day" format, but that isn't always the case.Specify the column type using the
colClasses
argument.debt <- read.csv("DebtPenny_20160909_20210909.csv", colClasses = c("Record.Date" = "Date"))
Convert to type date after reading the data.
debt$Record.Date <- as.Date(debt$Record.Date)