rinepry
rinepry

Reputation: 3

How should I format dates to make a time series line plot?

I have tibble occurrences

   count_ date_              
    <int> <dttm>             
 1     NA 1923-01-01 00:00:00
 2     NA 1923-01-01 00:00:00
 3     NA 1923-01-01 00:00:00
 4     NA 1923-01-01 00:00:00
 5      1 1923-03-23 00:00:00
 6      1 1923-03-23 00:00:00
 7      1 1923-03-23 00:00:00
 8      1 1923-03-23 00:00:00
 9      1 1923-03-26 00:00:00
10      1 1923-03-26 00:00:00

I try to fix the nulls in count_ and the format in date_...

occurrences <- occurrences %>%
  mutate(
    count_ = replace_na(count_, 1),
    date_ = as_date(date_)
  )
head(occurrences)
------------------------------------------------------------------------
   count_ date_     
    <int> <date>    
 1      1 1923-01-01
 2      1 1923-01-01
 3      1 1923-01-01
 4      1 1923-01-01
 5      1 1923-03-23
 6      1 1923-03-23
 7      1 1923-03-23
 8      1 1923-03-23
 9      1 1923-03-26
10      1 1923-03-26

Then I try to make a geom_line plot with the mutated fields...

ggplot(occurrencesTibble, mapping = aes(date_, count_)) +
  geom_line()

But I get this with bars instead of a line.

I have tried to change the fields to decimals and numerics, so that they are plotted continuously, but I keep getting the wrong type of chart.

Upvotes: 0

Views: 66

Answers (0)

Related Questions