Error in seq.int(0, to0 - from, by) : 'to' must be a finite number - Error in ggplot

I want to plot a line graph of multiple states of India affected by COVID19. The code is as follows:

swd <- data.frame(read.csv(url("https://api.covid19india.org/csv/latest/state_wise_daily.csv"),stringsAsFactors = F))
swd$Date <- anydate(swd$Date)
swd$Date <- as.Date(swd$Date,format = "%y/%m/%d")

swc <- swd %>% filter(Status == "Confirmed")
b <- ggplot(swc, aes(x = Date)) + geom_line(aes(y = MH, color = "darkred")) + geom_line(aes(y = GJ, color = "steelblue")) + geom_line(aes(y = TN, color = "yellow")) + geom_line(aes(y = RJ, color = "green")) + geom_line(aes(y = DL, color = "orange")) + geom_line(aes(y = UP, color = "pink")) + geom_line(aes(y = MP, color = "red")) + geom_line(aes(y = WB, color = "blue")) + geom_line(aes(y = BR)) + geom_line(aes(y = KL, color = "teal"))
ggplotly(b)

But I get the following error:
Error in seq.int(0, to0 - from, by) : 'to' must be a finite number

I don't know how to solve it, Please help!!

It looks to me like the issue is in your date conversion. See below for an approach to converting the strings to dates using the lubridate package. As an aside, you may want to think about reshaping your data from wide to long before you plot it -- in general, ggplot2 works much better with long instead of wide data.

library(tidyverse)
library(lubridate)

swd <- read_csv(url("https://api.covid19india.org/csv/latest/state_wise_daily.csv"))

swc <- swd %>%
  filter(Status == "Confirmed") %>% 
  mutate(Date = dmy(Date))

ggplot(swc, aes(x = Date)) +
  geom_line(aes(y = MH, color = "darkred")) +
  geom_line(aes(y = GJ, color = "steelblue")) +
  geom_line(aes(y = TN, color = "yellow")) +
  geom_line(aes(y = RJ, color = "green")) +
  geom_line(aes(y = DL, color = "orange")) +
  geom_line(aes(y = UP, color = "pink")) +
  geom_line(aes(y = MP, color = "red")) +
  geom_line(aes(y = WB, color = "blue")) +
  geom_line(aes(y = BR)) +
  geom_line(aes(y = KL, color = "teal"))

Created on 2020-06-10 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.