ggplot how show data for the last month period

Viewed 220

I have this piece of code. My goal is plot data for the last month. I read on the web that it is possible with scale_x_date(limits = as.Date So 1st step I tried plot data in specified timeframe... e.g. 2020-05-01","2020-09-09 but when I insert this code to my ggplot I got this error Invalid input: date_trans works with objects of class Date only Any idea what is wrong ?

Dataframe example: read from csv

"date","diff"
2020-01-22,555
2020-01-23,99
2020-01-24,287
2020-01-25,493
...

Code:

##before plot I had to  convert date from posix time, to show properly date in plot
conf_all_data$date <- as.POSIXct(conf_all_data$date)

##ggplot
      
  ggplot(conf_all_data, aes(x=date, y=diff, text=paste(date, "\n" ,  "Confirmed:", diff ))) + 
  geom_bar(stat='identity',fill='blue')  +
  scale_x_date(limits = as.Date(c("2020-05-01","2020-09-09"))) +
  scale_y_continuous(labels = comma),
  tooltip = "text"

One comment added here: If date is not date as it looks like, why then ggplot has no problem with correct plotting this chart also with X date Axis ? You can check my working site here http://webcovid19.online/, code is in all charts the same, only I removed problematic part with scale_x_date, I have added also screenshot enter image description here

Thanks to all, issue was really wrong Date type, when I did debug in console, I realized that after read csv to DF is factor type, then I have to convert with As.date()

Anyway my question is if I want show plot of last month period dataframe, e.g. today we have 2020-12-08, Is there some Date function with arithmetic operations to get minus 30 days ( or another number) from today to deal with months crossing ?

Here is my working screenshot with specified working timeframe

  ggplot(slovakia_conf_inc, aes(x=date, y=confirmed, text=paste( date,  "\n",  "confirmed:", confirmed ))) + 
        geom_bar(stat='identity',fill='blue')  +
        scale_x_date(breaks = "1 month") +
        scale_x_date(limits = as.Date(c("2020-08-01","2020-09-09"))) +
        scale_y_continuous(labels = comma),
      tooltip = "text"

enter image description here

3 Answers

Using your data, first try this:

library(ggplot2)
#Transform
conf_all_data$date <- as.Date(conf_all$date)
#Code
ggplot(conf_all_data, aes(x=date, y=diff,
                          label=paste(date, "\n" ,  "Confirmed:", diff ))) + 
  geom_bar(stat='identity',fill='blue')  +
  scale_y_continuous(labels = scales::comma)

Output:

enter image description here

Because your date's are not type of Date.

Take for example this code with the economics dataset and your limit dates:

ggplot(data = economics) +
  geom_line(aes(x = date, y = unemploy), 
            color = "#09557f",
            alpha = 0.6,
            size = 0.6) +
  labs(x = "Date", 
       y = "US Unemployed in Thousands",
       title = "Base Plot") +
   scale_x_date(limits = as.Date(c("1999-05-01","1999-09-09"))) +
  theme_minimal()

enter image description here

The error you are getting is similar to trying to plot geom_line(aes(x = as.character(date), y = unemploy). Which throws an enter image description here

Use the as.Date function to fix it.

OK, I just found solution for Subtraction Date with lubridate. e.g.

library(lubridate)
date <- As.Date("2009-10-01")


ymd(date) - 5
# [1] "2009-09-26"
Related