Home > OS >  Plotting a time series with ggplot geom_line
Plotting a time series with ggplot geom_line

Time:09-17

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")

Unemployment Plot

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 Debt plot

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.

  1. 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.

  2. Specify the column type using the colClasses argument.

    debt <- read.csv("DebtPenny_20160909_20210909.csv", colClasses = c("Record.Date" = "Date"))
    
  3. Convert to type date after reading the data.

    debt$Record.Date <- as.Date(debt$Record.Date)
    
  • Related