kalidogg
kalidogg

Reputation: 5

Plotting a time series with ggplot geom_line

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 https://fiscaldata.treasury.gov/datasets/debt-to-the-penny/debt-to-the-penny.

library(tidyverse)
library(dplyr)
data(debt)

debt <- read.csv("C:##path here to the data")

debt %>%
  filter(Calendar.Month.Number==12 & Calendar.Day.Number==31) %>% 
  ggplot(aes(Calendar.Year, Debt.Held.by.the.Public)) +
  geom_line(col = "blue")

Debt plot

What should I be doing differently?

Upvotes: 0

Views: 242

Answers (1)

neilfws
neilfws

Reputation: 33782

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)
    

Upvotes: 0

Related Questions