1

I am working on a project with Covid data, and I wish to plot (and later compare) covid data from three countries: Netherlands, Germany, France all on the same plot.

I wish to name the x-axis "Date", the y-axis "Number of confirmed cases" and the title of the legend "COVID Cases in Countries".

So far, I have created a final data frame with the data from all three counties called "dfFinal". The ggplot I included does not work (because I dont know how to code it). I am just having trouble plotting the information. Could someone help? The data has been gathered an read using Jsonlite, and is easily re-created without needing a data-set.

structure(list(Country = c("Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands"), Date = structure(c(18317, 18318, 18319, 18320, 18321, 18322, 18323, 18324, 18325, 18326, 18327, 18328, 18329, 18330, 18331), class = "Date"), Cases = c(0L, 1L, 6L, 10L, 18L, 24L, 38L, 82L, 128L, 188L, 265L, 321L, 382L, 503L, 603L)), row.names = 36:50, class = "data.frame")

install.packages("jsonlite", dependencies = TRUE)
library("jsonlite")

tmp <- readLines("https://pomber.github.io/covid19/timeseries.json")
jsonLot <- fromJSON(tmp)

myjsonLot <- toJSON(jsonLot, pretty=TRUE)
fileConn <- file(paste0("Covid19_timeseries.json"))
writeLines(myjsonLot, fileConn)


dfN <- data.frame(Country = c("Netherlands"), Date = c(jsonLot$Netherlands$date), Cases = 
c(jsonLot$Netherlands$confirmed))
dfN[,]

dfG <- data.frame(Country = c("Germany"), Date = c(jsonLot$Germany$date), Cases = 
c(jsonLot$Germany$confirmed))
dfG[,]

dfF <- data.frame(Country = c("France"), Date = c(jsonLot$France$date), Cases = 
c(jsonLot$France$confirmed))
dfF[,]

dfFinal <- rbind.fill(dfN, dfG, dfF)
View(dfFinal)

ggplot(dfFinal, aes(x=Date, y=Cases) + geom_line(size=1) +
xlab("Date") + ylab("Number of confirmed cases") + scale_x_datetime(breaks = "1 day") +
geom_line(aes(y=dfN$Cases), colour ="red") +
geom_line(aes(y=dfG$Cases), colour ="red") +
geom_line(aes(y=dfF$Cases), colour ="red"))
3
  • Please share your data using dput(df)? Commented Apr 27, 2022 at 15:45
  • structure(list(Country = c("Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands", "Netherlands"), Date = structure(c(18317, 18318, 18319, 18320, 18321, 18322, 18323, 18324, 18325, 18326, 18327, 18328, 18329, 18330, 18331), class = "Date"), Cases = c(0L, 1L, 6L, 10L, 18L, 24L, 38L, 82L, 128L, 188L, 265L, 321L, 382L, 503L, 603L)), row.names = 36:50, class = "data.frame") Commented Apr 27, 2022 at 15:59
  • Please share your data in your question above. Commented Apr 27, 2022 at 16:00

1 Answer 1

2

Your code does not work, because you are supposed to add other calls like geom_line() or xlab() to ggplot() outside, not inside. I fixed it:

dfFinal$Date <- as.POSIXct(dfFinal$Date)

ggplot(dfFinal, aes(x=Date, y=Cases, group=Country)) + 
  geom_line(size=1) +
  xlab("Date") + 
  ylab("Number of confirmed cases") + 
  scale_x_datetime(breaks = "1 day")

I also added transformation of the Date variable, because when you read JSON it is not in a proper format.

However, your style of code is not very great. I recreated rest of your data transformation in a way which is more compliant with tidy R code standards.

library(jsonlite)
library(dplyr)  # for data transformation and piping
library(purrr)  # for mapping and binding
library(ggplot2)

# combine function calls by piping them, see help for `%>%` for details
dat <- readLines("~/../Desktop/timeseries.json") %>% fromJSON() 

# for each of the data frames in the json append the name of the data.frame as 
# "Country" variable and then bind all of them 
dat <- imap_dfr(dat, ~ .x, .id = "Country") %>%
  mutate(Date = as.POSIXct(date)) %>% # transform date into a proper format
  filter(Country %in% c("Netherlands", "France", "Germany")) # filter data by focus countries

ggplot(dat, aes(x = Date, y = confirmed, group = Country, color = Country)) + 
  geom_line(size=1) +
  ggtitle("Number of COVID-19 casses in selected countries") +
  xlab("Date") + 
  ylab("Number of confirmed cases") 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.