Sarah
Sarah

Reputation: 27

Changing period of dates to standard date to do line graph

I'm trying to plot a line graph with R using the dataset that can be found here . I'm looking specifically at how to plot the number of cases in each region i.e. north east, north west etc against the period of time.

However, as the date is a period of a week rather than a standard date, how can I convert it to make the line graph actually possible? For example, right now it has the dates as 01/09/2020 - 07/09/2020. How can I use this for a line graph?

Sorry if my explanation isn't clear, here is a picture below.

enter image description here

Upvotes: 0

Views: 91

Answers (1)

Sabrina Xie
Sabrina Xie

Reputation: 108

I assume you're trying to plot a time series? You could just trim the dates to the beginning of the week and label the time axis as "Week beginning on date". You could do this with substr() in base r and keep the first 10 characters.

substr(data$column,1,10)

You may also want to format it as a date, easiest with the lubridate package, something like dmy() (day month year).

Here is the full code you would want:

library(tidyverse)

#Read in data
data <- read.csv("/Users/sabrinaxie/Downloads/covid19casesbysociodemographiccharacteristicengland1sep2020to10dec20213.csv")

#Modify data and remove extraneous top rows
data <- data %>% 
  rename(Period=Table.9..Weekly.estimates.of.age.standardised.COVID.19.case.rates..per.100.000.person.weeks..by.region..England..1.September.2020.to.6.December.20211.2.3) %>% 
  slice(3:n())

#Keep first 10 characters of Period column and assign to old column to replace
data$Period <- substr(data$Period,1,10)

#Parse as date
data$Period <- dmy(data$Period)

Upvotes: 1

Related Questions